[英]Angular ngIf router has /id ( /a dynamic number )
I have created this in the main.ts which gets the route url:我在获取路由 url 的 main.ts 中创建了这个:
router: string;
constructor(private _router: Router) {
this.router = _router.url;
}
This works well.这很好用。 Then in the main.html:
然后在 main.html 中:
<app-main-menu *ngIf="router == '/'"></app-main-menu>
<app-item-list *ngIf="router == '/list'"></app-item-list>
<app-item-info *ngIf="router == '/:id'"></app-item-info> <!-- THIS DOES NOT WORK -->
When the route is /2
or /35
(or any number) I cannot get the app-item-info ngIf
to work.当路线是
/2
或/35
(或任何数字)时,我无法让 app-item-info ngIf
工作。
My question is, how can I set the *ngIf to show (true) when I always have a string " / " with a dynamic number " 345 " ?我的问题是,当我总是有一个带有动态数字“345”的字符串“/”时,如何将 *ngIf 设置为显示(真)?
Example: When the URL (route is ): localhost:4200/2
, the ngIf
should display app-item-info.示例:当 URL (route is ):
localhost:4200/2
, ngIf
应显示 app-item-info。
EDIT: I cannot use <router-outlet>
as I am using it for a different thing.编辑:我不能使用
<router-outlet>
因为我将它用于不同的事情。 Showing pages.显示页面。
I recommend to use <router-outlet>
to implement this functionality.我建议使用
<router-outlet>
来实现这个功能。 Check out the official Angular documentation about the routing: https://angular.io/guide/router查看有关路由的官方 Angular 文档: https : //angular.io/guide/router
Live example: https://stackblitz.com/angular/aqqyljyojye实例: https : //stackblitz.com/angular/aqqyljyojye
Can this help you?这能帮到你吗?
<app-item-info *ngIf="isItemInfoRoute(router)"></app-item-info>
and in .ts并在 .ts
isItemInfoRoute(router: string) {
return router.search(/^\/\d+/) > -1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.