Bird
Raised Fist0
Angularframework~10 mins

Route parameters with ActivatedRoute in Angular - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Route parameters with ActivatedRoute
User navigates to URL with parameter
↓
Angular Router matches route
↓
Component loads
↓
ActivatedRoute reads parameter
↓
Component uses parameter value
↓
Display or process parameter in template
When a user visits a URL with a parameter, Angular Router loads the component and ActivatedRoute reads the parameter so the component can use it.
Execution Sample
Angular
constructor(private route: ActivatedRoute) {}

ngOnInit() {
  this.route.paramMap.subscribe(params => {
    this.id = params.get('id');
  });
}
This code listens for the 'id' parameter from the route and saves it in the component.
Execution Table
StepActionActivatedRoute.paramMapParameter 'id'Component Property 'id'
1Component initializedparamMap observable creatednullundefined
2User navigates to /items/42paramMap emits new params'42'undefined
3Subscription callback runsparamMap current value'42''42' assigned
4Component template updatesparamMap unchanged'42''42' displayed
5User navigates to /items/100paramMap emits new params'100''42'
6Subscription callback runsparamMap current value'100''100' assigned
7Component template updatesparamMap unchanged'100''100' displayed
8User leaves routeparamMap completes or unsubscribednull'100'
💡 User leaves route or component destroyed, subscription ends
Variable Tracker
VariableStartAfter Step 3After Step 6Final
paramMapobservable createdemits params with id='42'emits params with id='100'subscription ends
id (component property)undefined'42''100''100'
Key Moments - 2 Insights
Why do we use subscribe on paramMap instead of reading it once?
Because route parameters can change while the component is active, subscribing lets us react to each change as shown in steps 3 and 6 of the execution_table.
What happens if the parameter 'id' is missing in the URL?
The paramMap.get('id') returns null, so the component property 'id' becomes null, as paramMap emits new values only when parameters change.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'id' after step 3?
A'42'
B'100'
Cundefined
Dnull
💡 Hint
Check the 'Component Property id' column at step 3 in the execution_table.
At which step does the component update 'id' to '100'?
AStep 2
BStep 4
CStep 6
DStep 8
💡 Hint
Look for when 'id' changes from '42' to '100' in the execution_table.
If the user navigates to a URL without 'id', what will paramMap.get('id') return?
A'0'
Bnull
Cundefined
Dempty string
💡 Hint
Refer to key_moments about missing parameters and paramMap behavior.
Concept Snapshot
Route parameters let components get values from the URL.
Use ActivatedRoute.paramMap.subscribe() to watch for changes.
Access parameters with paramMap.get('paramName').
Update component properties inside subscription.
This keeps the component in sync with URL changes.
Full Transcript
When a user visits a URL with a parameter, Angular Router loads the matching component. The component uses ActivatedRoute to access the route parameters. ActivatedRoute has a paramMap observable that emits the current parameters. The component subscribes to paramMap to get notified when parameters change. Inside the subscription callback, the component reads the parameter value using paramMap.get('id') and saves it to a property. This property can then be used in the template to display or process the parameter. If the user navigates to a different URL with a new parameter, the subscription callback runs again with the new value. If the parameter is missing, paramMap.get returns null. This approach ensures the component always has the latest parameter value while it is active.

Practice

(1/5)
1. What is the primary purpose of ActivatedRoute in Angular?
easy
A. To create new routes dynamically at runtime
B. To access route parameters and query parameters inside a component
C. To navigate programmatically between routes
D. To define routes in the Angular application

Solution

  1. Step 1: Understand the role of ActivatedRoute

    ActivatedRoute is used inside a component to access information about the current route, including parameters.
  2. Step 2: Differentiate from other routing services

    Defining routes is done in RouterModule, navigation uses Router service, not ActivatedRoute.
  3. Final Answer:

    To access route parameters and query parameters inside a component -> Option B
  4. Quick Check:

    ActivatedRoute = access route info [OK]
Hint: ActivatedRoute reads URL info inside components [OK]
Common Mistakes:
  • Confusing ActivatedRoute with Router for navigation
  • Thinking ActivatedRoute defines routes
  • Using ActivatedRoute to create routes dynamically
2. Which of the following is the correct way to subscribe to route parameters using ActivatedRoute?
easy
A. this.route.param.subscribe(params => { const id = params['id']; });
B. this.route.params.get('id').subscribe(id => { });
C. this.route.paramMap.subscribe(params => { const id = params.get('id'); });
D. this.route.getParam('id').subscribe(id => { });

Solution

  1. Step 1: Identify correct ActivatedRoute property

    The correct property to get parameters as an observable is paramMap, not params or others.
  2. Step 2: Use the correct method to get parameter

    From paramMap, use get('id') to retrieve the parameter value.
  3. Final Answer:

    this.route.paramMap.subscribe(params => { const id = params.get('id'); }); -> Option C
  4. Quick Check:

    paramMap + get() = correct subscription [OK]
Hint: Use paramMap.subscribe and get('paramName') [OK]
Common Mistakes:
  • Using this.route.params.get instead of paramMap
  • Trying to subscribe directly on params.get()
  • Using non-existent getParam method
3. Given this code snippet inside a component:
constructor(private route: ActivatedRoute) { }

ngOnInit() {
  this.route.paramMap.subscribe(params => {
    this.userId = params.get('userId');
  });
}

If the URL is /profile/42 and the route is defined as { path: 'profile/:userId' }, what will be the value of this.userId after initialization?
medium
A. '42'
B. 42 (number)
C. null
D. undefined

Solution

  1. Step 1: Understand paramMap.get returns string

    The paramMap.get() method always returns a string or null if not found.
  2. Step 2: Check route and URL match

    The route profile/:userId matches URL /profile/42, so userId is '42'.
  3. Final Answer:

    '42' -> Option A
  4. Quick Check:

    paramMap.get returns string '42' [OK]
Hint: paramMap.get returns string, not number [OK]
Common Mistakes:
  • Assuming paramMap.get returns number type
  • Expecting undefined if parameter exists
  • Confusing paramMap with params object
4. Consider this code inside a component:
ngOnInit() {
  this.route.paramMap.subscribe(params => {
    this.id = params.get('id');
  });
}

But the URL is /product with no id parameter. What issue will occur?
medium
A. this.id will be null, no error occurs
B. Runtime error because 'id' parameter is missing
C. Subscription will not trigger at all
D. this.id will be undefined and cause template error

Solution

  1. Step 1: Understand paramMap.get behavior when param missing

    If the parameter does not exist, paramMap.get() returns null, not an error.
  2. Step 2: Check subscription behavior

    The subscription still triggers with the current paramMap, so this.id becomes null.
  3. Final Answer:

    this.id will be null, no error occurs -> Option A
  4. Quick Check:

    Missing param returns null, no error [OK]
Hint: Missing param returns null, not error [OK]
Common Mistakes:
  • Expecting runtime error if param missing
  • Thinking subscription won't trigger
  • Confusing undefined with null
5. You want to create a component that updates its displayed data whenever the route parameter postId changes without recreating the component. Which approach correctly handles this using ActivatedRoute?
hard
A. Read postId once in the constructor and store it
B. Reload the entire component manually on route change
C. Use snapshot.paramMap.get('postId') only in ngOnInit
D. Subscribe to paramMap in ngOnInit and update data inside the subscription callback

Solution

  1. Step 1: Understand component reuse on route param change

    Angular reuses the component instance when only route parameters change, so ngOnInit runs once.
  2. Step 2: Use paramMap subscription to detect param changes

    Subscribing to paramMap lets you react to parameter changes dynamically without recreating the component.
  3. Step 3: Why snapshot is insufficient

    snapshot.paramMap reads parameters only once and won't update if params change while component is active.
  4. Final Answer:

    Subscribe to paramMap in ngOnInit and update data inside the subscription callback -> Option D
  5. Quick Check:

    Subscribe paramMap for dynamic param changes [OK]
Hint: Subscribe to paramMap to handle param changes live [OK]
Common Mistakes:
  • Using snapshot only and missing param updates
  • Reading param once in constructor
  • Manually reloading component instead of subscribing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard