0
0
Angularframework~20 mins

Route parameters with ActivatedRoute in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ActivatedRoute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does ActivatedRoute provide route parameters?
Consider an Angular component that uses ActivatedRoute to get route parameters. Which option correctly describes how to access a parameter named id synchronously?
Angular
constructor(private route: ActivatedRoute) {}

ngOnInit() {
  // How to get 'id' parameter synchronously?
}
Aconst id = this.route.snapshot.paramMap.get('id');
Bconst id = this.route.params['id'];
Cconst id = this.route.paramMap.get('id');
Dconst id = this.route.snapshot.params.id;
Attempts:
2 left
💡 Hint
Use the snapshot property for synchronous access and paramMap for safer parameter retrieval.
state_output
intermediate
2:00remaining
What is the output when subscribing to route params?
Given this Angular component code, what will be logged when the route parameter id changes from '5' to '10'?
Angular
constructor(private route: ActivatedRoute) {}

ngOnInit() {
  this.route.params.subscribe(params => {
    console.log(params['id']);
  });
}
A5 then 10
BOnly 10
COnly 5
DNo output
Attempts:
2 left
💡 Hint
Subscribing to params emits every time the route parameters change.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in accessing route parameters
Which option contains a syntax error when trying to get the userId parameter from ActivatedRoute?
Angular
constructor(private route: ActivatedRoute) {}

ngOnInit() {
  const userId = ???
}
Aconst userId = this.route.snapshot.paramMap.get('userId');
Bconst userId = this.route.paramMap.get('userId');
Cconst userId = this.route.snapshot.paramMap.userId;
Dconst userId = this.route.snapshot.params['userId'];
Attempts:
2 left
💡 Hint
paramMap is a map with a get() method, not a plain object.
🔧 Debug
advanced
2:00remaining
Why does subscribing to params not update the component property?
In this Angular component, the userId property does not update when the route parameter changes. What is the likely cause?
Angular
userId: string | null = null;

constructor(private route: ActivatedRoute) {}

ngOnInit() {
  this.route.params.subscribe(params => {
    params['userId'];
  });
}
AThe <code>params</code> observable does not emit on parameter changes.
BThe subscription callback does not assign the parameter to <code>userId</code>.
CThe <code>userId</code> property must be declared as a number, not string.
DThe <code>ActivatedRoute</code> service is not injected properly.
Attempts:
2 left
💡 Hint
Check if the parameter value is assigned to the component property inside the subscription.
🧠 Conceptual
expert
3:00remaining
Why use paramMap over params in ActivatedRoute?
Which reason best explains why Angular recommends using paramMap instead of params to access route parameters?
A<code>params</code> only works for query parameters, not route parameters.
B<code>params</code> is deprecated and no longer emits values on parameter changes.
C<code>paramMap</code> automatically converts parameters to numbers, while <code>params</code> returns strings.
D<code>paramMap</code> provides a consistent API with methods like <code>get()</code> and handles missing parameters gracefully.
Attempts:
2 left
💡 Hint
Think about API consistency and handling of missing parameters.