在我的模板中,我有这个导航来检查ngIf router.url is = 到当前网址,我想在我的导航中显示其他链接。
问题是我不知道是否有可能以我试图的方式做到这一点。如何正确编写语法,以便我可以在模板中获取此动态 id。我尝试了很多方法:
like so - '/csgo/match/${id}'
'/csgo/match/id'
'/csgo/match/:id'
<ng-template [ngIf]="router.url === '/csgo/match/${id}'">
<a class="nav-item nav-link" routerLink="csgo/results">Results</a>
<a class="nav-item nav-link" routerLink="csgo/ranking">World Ranking</a>
<a class="nav-item nav-link" routerLink="csgo/upcoming">Upcoming</a>
</ng-template>
路由模块
{ path: '', component: HomeRouterComponent },
{ path: 'csgo', component: CsgoComponent},
{ path: 'csgo/results', component: CsResultsComponent},
{ path: 'csgo/ranking', component: RankingComponent},
{ path: 'csgo/match/:id', component:MatchComponent},
{ path: 'csgo/team/:id', component: TeamPageComponent},
{ path: 'csgo/upcoming', component:UpcomingLiveComponent},
<ng-template *ngIf="showRoutes">
<a class="nav-item nav-link" routerLink="csgo/results">Results</a>
<a class="nav-item nav-link" routerLink="csgo/ranking">World Ranking</a>
<a class="nav-item nav-link" routerLink="csgo/upcoming">Upcoming</a>
</ng-template>
在你的.ts
,
import {ActivatedRoute} from '@angular/router';
constructor(private _route : ActivatedRoute){}
订阅您的路由参数:
this._route.paramMap.subscribe((params)=>{
//change the value of showRoutes based on your requirements
});
您的模板 *ngIf 条件应如下所示: ${id} 应该在报价之后。
<ng-template [ngIf]="router.url === '/csgo/match/'${id)">
[ngIf]="router.url === '/csgo/match/' +id"