Angular 2 navigating in the different instances of the same component with different data to display

三世轮回 提交于 2019-12-14 02:26:35

问题


I have a simple side menu in my Angular 2 applications which display a list of classRoom, each class Room has many users.

I navigate between the different instance with routerLink.

My problem is that when I click on the first item : -class Room number 1 I have no problem and the list of user are alright, but when I click on Class Room number 2 the data does not change, so the users displayed are the users of Class Room Number 2.

How can I tell Angular To re-render the component with the new data?
Any suggestions?

UPDATE CODE EXAMPLE

side-menu.component.html

<li *ngFor='let classRoom of classRooms'>
    <li>
        <a [routerLink]="['/app/users/', classRoom.name]">**Users**</a>
    </li>
    <li>....</li>
    <li>....</li>
</li>

routes.ts

....
{ path: 'app/users/:classroom', component: usersComponent},

So when i click on 'users' link, i call the users component, get classroom's name by route param and make a get request to get the users.

With the first class room no problem, but if i click on the 'users'link of the second classroom no change happend.


回答1:


I see you are loading userComponent in your route.ts. Can you try like below:

<div class="col-md-3" *ng-for="let classRoom of classRooms">
    <a [routerLink]="['/app/users/', classRoom.name]">
       <user-component></user-component>
    </a>
</div>



回答2:


Your routerLink needs a terminal route

[routerLink]="['parent', 'child', query.id]"

to understand it more clear, see this post see my question and answer



来源:https://stackoverflow.com/questions/40614263/angular-2-navigating-in-the-different-instances-of-the-same-component-with-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!