7

In Angular2 RC1 and lower calling a route link always causes a component to reload:

<a [routerLink]="['/page', {id: 1}]">A link </a>

Using Angular2, none RC, the component isn't reloaded if its navigating to itself with different parameters. Is there any way to get the reload behavior back?

I understand the other way of dealing with this, subscribing from the ActivatedRoute and detected variable changes, but this causes the component logic to increase in complexity.

3
  • 1
    That's not supported in the new router, but I saw discussions about plans to support that eventually. Commented Oct 21, 2016 at 4:50
  • 1
    How did you accomplish this ? Commented Nov 4, 2016 at 8:20
  • Could you please explain on how to achieve that functionality ? I'm facing the exact same problem, I have a single component linked to multiple routes, which gets loaded with different server data based on the current route. Commented Nov 7, 2016 at 13:14

2 Answers 2

3

Although you've mentioned 'ActivatedRoute' and how it will complex your code, I think that this will help other people that are facing this problem when they're reaching your question, like me :) .

This topic will answer your question.

This code below (pasted from the topic above), is where the 'magic' is happening. If you'll place your 'reload' code in this subsribe function, your component will reload.

ngOnInit() {
   this.sub = this.route.params.subscribe(params => {
      this.id = +params['id']; // (+) converts string 'id' to a number
      // Some 'reload' coding
      // In a real app: dispatch action to load the details here.
   }); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

A simple way to do this is to force the template to reload using *ngIf. You would need a variable that "turns the component off and on". If the values of variables have changed before this off/on behaviour, they will be rendered when the view is on again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.