Query parameters and fragments help you send extra information in URLs. They let your app show different content or jump to parts of a page.
Query parameters and fragments in Angular
this.router.navigate(['/path'], { queryParams: { key: 'value' }, fragment: 'section' });
queryParams is an object with key-value pairs for the URL query string.
fragment is a string that points to a part of the page after the # symbol.
this.router.navigate(['/products'], { queryParams: { category: 'books', sort: 'price' } });
this.router.navigate(['/profile'], { fragment: 'settings' });
this.router.navigate(['/search'], { queryParams: { q: 'angular' }, fragment: 'results' });
This component shows how to navigate with query parameters and fragments. When you click the button, it goes to /products?category=books#top. It also listens to the URL and shows the current category and fragment on the page.
import { Component } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-query-example', template: ` <h2>Query Params and Fragment Example</h2> <button (click)="goToPage()">Go to Products with Filters</button> <p>Current category: {{ category }}</p> <p>Current fragment: {{ fragment }}</p> ` }) export class QueryExampleComponent { category = ''; fragment = ''; constructor(private router: Router, private route: ActivatedRoute) { this.route.queryParams.subscribe(params => { this.category = params['category'] || 'none'; }); this.route.fragment.subscribe(frag => { this.fragment = frag || 'none'; }); } goToPage() { this.router.navigate(['/products'], { queryParams: { category: 'books' }, fragment: 'top' }); } }
Query parameters and fragments do not reload the whole page but update the URL and app state.
Use ActivatedRoute to read query parameters and fragments inside your components.
Fragments usually scroll the page to the matching element with the same id.
Query parameters add extra info after ? in URLs to filter or customize views.
Fragments add a # part to jump to sections inside a page.
Angular's router lets you set and read these easily with navigate and ActivatedRoute.
Practice
Solution
Step 1: Understand URL structure
Query parameters come after a question mark (?) in URLs and carry extra data.Step 2: Role in Angular routing
Angular uses query parameters to pass optional info without changing the main route path.Final Answer:
To pass extra information to the route without changing the path -> Option DQuick Check:
Query parameters = extra info in URL [OK]
- Confusing query parameters with route parameters
- Thinking query parameters change the route path
- Mixing fragments with query parameters
Solution
Step 1: Recall Angular Router navigation syntax
The correct method isnavigatewith an array for the path and an options object.Step 2: Identify correct query parameter option
The option to pass query parameters isqueryParams, notparamsorquery.Final Answer:
router.navigate(['/path'], { queryParams: { key: 'value' } }) -> Option AQuick Check:
Use navigate() with queryParams [OK]
- Using wrong method like go() or change()
- Passing query parameters as params or query instead of queryParams
- Not wrapping path in an array
this.route.queryParams.subscribe(params => console.log(params['page']))
this.router.navigate(['/items'], { queryParams: { page: 3 } })Solution
Step 1: Understand queryParams subscription
The subscription listens for query parameter changes and logs the 'page' value.Step 2: Navigate with queryParams
The navigate call sets queryParams with page: 3, triggering the subscription.Final Answer:
3 -> Option BQuick Check:
queryParams['page'] logs 3 after navigation [OK]
- Expecting undefined because of timing
- Confusing queryParams with route params
- Not subscribing to queryParams observable
this.route.fragment.subscribe(fragment => {
console.log(fragment.toUpperCase())
})Solution
Step 1: Understand fragment observable behavior
The fragment can be null if no fragment exists in the URL.Step 2: Identify risk of calling toUpperCase()
Calling toUpperCase() on null causes a runtime error.Final Answer:
fragment can be null, so calling toUpperCase() may cause error -> Option CQuick Check:
Check for null before using string methods on fragment [OK]
- Assuming fragment is always a string
- Trying to get fragment from queryParams
- Not subscribing to fragment observable
{ tab: 'settings' } and fragment 'section2'. Which code correctly does this in Angular?Solution
Step 1: Recall correct options for navigate()
Angular usesqueryParamsfor query parameters andfragmentfor URL fragment.Step 2: Check option correctness
this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, fragment: 'section2' }) uses correct keys:queryParamsandfragment. Others use incorrect keys likeparams,query, orhash.Final Answer:
this.router.navigate(['/profile'], { queryParams: { tab: 'settings' }, fragment: 'section2' }) -> Option AQuick Check:
Use queryParams and fragment keys in navigate() [OK]
- Using params or query instead of queryParams
- Using hash instead of fragment
- Not passing path as array
Ready to prove it?
Pass the test and this pattern moves unknown
