Adam Bremen
Adam Bremen

Reputation: 695

Angular 2 Change route (URL) and Load data from DB

In my project I have menu which is inside *ngFor loop and where I have menu.Id as a parameter to load data from database

<li *ngFor="let item of menu">
  <a [routerLink]="['/products', item .Id]">{{item .Name}}</a>
</li>

when I click this menu item for first time (no matter which one) it works great but after that to click another menu item it not works because ProductsComponent is already loaded and cann't take Id again (route isn't changed '/products/:id' Id is different but constructor is already loaded and component is initialized).

In ProductsComponents' constructor getting data like this

 this.route.params.subscribe(params => {
      this.menuId = params.menuId;
 });

Upvotes: 0

Views: 344

Answers (1)

oeste
oeste

Reputation: 1559

You can just get the info you need from the route on load

For example, in your ngOnInit you can call getMenuId() with the function:

getMenuId() {
    const id = +this.route.snapshot.paramMap.get('menuId');
    //do something with menuId, like call another function to load your data with the id
  }

Upvotes: 1

Related Questions