2017-07-17 40 views
1

我有嵌套導航(路線)在我的例子如何防止父routerLink壓倒一切在孩子

<ul> 
    <li *ngFor="let route of routes" [routerLink]="route.link" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}"> 
     {{route.name}}</span> 
     <!-- Secondary navigation (if exists) --> 
     <ul *ngIf="route.children" class="secondary"> 
      <li *ngFor="let item of route.children" [routerLink]="item.link" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}"> 
      {{item.name}} 
      </li> 
     </ul> 
    </li> 
</ul> 

我每次點擊父項目,它定位到相應的鏈接,但在點擊其中任意一個子項,而不是相應的子鏈接,將定位於母公司<李>路由作爲整個兒<UL>是這個父<李> 我怎樣才能防止這種父routerLink在這種嵌套的情況下的一部分? Appriciate你的幫助。

+1

如果您將'(click)=「$ event.stopPropagation()」'添加到內部函數,這是否工作? –

+0

工作,謝謝 – SatAj

回答

1

當您單擊一個子元素時,它也觸發所有父元素上的click事件。由於它們按順序運行,最外面的一個在導航的情況下「獲勝」。

添加(click)="$event.stopPropagation()"(或使用(click)="someFunction($event)"並調用stopPropagation()裏面的功能,如果你需要在點擊運行其他邏輯也一樣)的子元素以防止起泡。