1

I'm implementing Master / Detail pattern as described in Angular2 tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt2.html

Simplified example:

import { Component } from '@angular/core';

export class Hero {
  id: number;
  name: string;
}

@Component({
  selector: 'my-app',
  template:`
    <ul>
      <li *ngFor="let hero of heroes" (click)="onSelect(hero)">
        {{hero.name}}
      </li>
    </ul>

    <div *ngIf="selectedHero">
      <h2>{{selectedHero.name}} details!</h2>
    </div>
  `,
})

export class AppComponent {
  heroes = HEROES;
  selectedHero: Hero;
  onSelect(hero: Hero) { this.selectedHero = hero; }
}

Whenever an item in a list is clicked its details are shown on the screen. However, I can't find a way to update url in the location bar to reflect the fact that one of the items is selected.

The only way seem to be to navigate to another view using router but it refreshes the whole master-detail component including the list of items.

My goal is only to refresh part of the page while keeping the url in sync with the shown content.

1 Answer 1

1

Not sure what you mean by "refreshes the whole page". That's exactly what the router does not.

If you use @angular/router-deprecated then you can implement CanReuse by adding

routerCanReuse(/*nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction*/) {
  return true;
}

This way the router doesn't drop and recreate the component when you navigate to the same route where only parameter values changed.

I don't think @angular/router supports this and @angular/router is going to be replaced again soon anyway.

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

3 Comments

I've updated the question - it refreshes whole master-detail component whenever router.navigate is called. I'm trying to avoid refresh of the list and only refresh item details.
If you change the route, then components are removed and others added. If the route doesn't change the component then routerCanReuse can avoid that components are dropped/recreated. Otherwise this is not supported. You can move the state of the component to a service to keep it when the component was destroyed and recreated.
Thank you, routerCanReuse was exactly what was required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.