Bird
0
0

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🚀 Application Q15 of 15
Angular - Routing
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?
ARead <code>postId</code> once in the constructor and store it
BReload the entire component manually on route change
CUse <code>snapshot.paramMap.get('postId')</code> only in <code>ngOnInit</code>
DSubscribe to <code>paramMap</code> in <code>ngOnInit</code> and update data inside the subscription callback
Step-by-Step Solution
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]
Quick Trick: Subscribe to paramMap to handle param changes live [OK]
Common Mistakes:
MISTAKES
  • Using snapshot only and missing param updates
  • Reading param once in constructor
  • Manually reloading component instead of subscribing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes