0
0
Angularframework~10 mins

Query parameters and fragments in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query parameters and fragments
User clicks link or navigates
Angular Router reads URL
Extract query parameters and fragment
Component subscribes to ActivatedRoute
Component receives updated params and fragment
Component updates view or logic accordingly
Angular reads the URL, extracts query parameters and fragments, then updates the component via ActivatedRoute subscriptions.
Execution Sample
Angular
this.route.queryParams.subscribe(params => {
  this.page = params['page'] || 1;
});

this.route.fragment.subscribe(fragment => {
  this.section = fragment || '';
});
Component listens to query parameters and fragment changes and updates its variables.
Execution Table
StepURLQuery Params ExtractedFragment ExtractedComponent Variables Updated
1/products?page=2#details{ page: '2' }detailspage=2, section='details'
2/products?page=3#reviews{ page: '3' }reviewspage=3, section='reviews'
3/products{}page=1 (default), section=''
4/products?page=5{ page: '5' }page=5, section=''
5/products#summary{}summarypage=1 (default), section='summary'
6Navigation ends---
💡 No more URL changes; subscriptions complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
pageundefined231511
sectionundefineddetailsreviewssummarysummary
Key Moments - 3 Insights
Why does 'page' become 1 when no query parameter is present?
Because the code uses 'params['page'] || 1' which sets page to 1 if 'page' is missing, as shown in execution_table row 3.
How does the component know when the fragment changes?
The component subscribes to 'this.route.fragment', so any fragment change triggers the subscription callback, updating 'section' as in rows 1, 2, and 5.
What happens if the URL has query parameters but no fragment?
The fragment subscription receives an empty string, so 'section' is set to '', as in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'page' after step 2?
A3
B2
C1
Dundefined
💡 Hint
Check the 'Component Variables Updated' column at step 2 in the execution_table.
At which step does the fragment become 'summary'?
AStep 3
BStep 5
CStep 4
DStep 1
💡 Hint
Look at the 'Fragment Extracted' column in the execution_table.
If the URL changes to '/products?page=10#top', what will 'section' be updated to?
A''
B10
Ctop
Dundefined
💡 Hint
Fragment updates the 'section' variable as shown in the variable_tracker and execution_table.
Concept Snapshot
Angular reads query parameters and fragments from the URL.
Use ActivatedRoute's queryParams and fragment observables.
Subscribe to get updates when URL changes.
Use defaults if parameters are missing.
Update component variables to reflect current URL state.
Full Transcript
In Angular, when a user navigates or clicks a link, the router reads the URL and extracts query parameters and fragments. The component subscribes to these values using ActivatedRoute's queryParams and fragment observables. When these values change, the component updates its variables accordingly. For example, if the URL is '/products?page=2#details', the component sets page to 2 and section to 'details'. If a parameter is missing, a default value can be used, like page defaulting to 1. This reactive approach keeps the component in sync with the URL state.