17

why ActivatedRoute in @angular\router params is observable ?

I'm wondering why do I need to subscribe for Params ? and why params are async is there a case the componenet of the route will be created but the params still not populated ?

and is there a way to just get the value without an observable ?

2 Answers 2

19

You can use

route.routerState.params 

You can subscribe because when your application navigates to the same route with just another parameter value, the component is not recreated and you have no way of knowing when the parameters have changes.

With subscribing to parameters you get notified, that a route action has happened where only the parameter value changed.

Sign up to request clarification or add additional context in comments.

4 Comments

what about ngOnInit ? will it be invoked once the component is created or for each time the component is active ?
only once . . . .
I believe now you have to use route.snapshot.params['param_name'], where route is an ActivatedRoute instance.
That's quite an old answer and the router was replaced several times since then. Thanks for the update.
6

The matrix parameters scoped to this route. The observable will emit a new value when the set of the parameters changes.

The reason that the params property on ActivatedRoute is an Observable is that the router may not recreate the component when navigating to the same component. In this case the parameter may change without the component being recreated.

1 Comment

The second part of your answer is the actual reason why and is exactly what I was looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.