Tom Hackett
Tom Hackett

Reputation: 11

ngOnInit not called when navigating to route parameter

I've been having this issue for a while now with every version of Angular I've used, but have fudged my way around it (currently on 6.0.3). I've got routes setup like this:

/book/:param

And I'm reading the parameter in ngOnInit fine, I can display it in the template etc. After that I have a quick way of setting a pretty version of the parameter which I actually want to display but this is never updated after the first time either route is navigated to. The same goes for anything I do differently for each route.

Should I just make separate components instead of using parameters or am I missing something obvious?

Upvotes: 1

Views: 177

Answers (1)

Pardeep Jain
Pardeep Jain

Reputation: 86730

No need to write routing configuration like this -

/book/:all
/book/:notconfirmed

Just replace this with a single line like this -

/book/:flag

Now what you have to do is, Just subscribe to params coming from routing and do whatever you want to do.

this.activatedRoute.params.subscribe(params => {
   console.log('every time route params chnages you will get here');
}

Upvotes: 2

Related Questions