Angular2-* ng如果路线是某个参数 [英] Angular2 - *ngIf route is a certain param
问题描述
我有一条路线/main/item:id
I have a route /main/item:id
类似于:
http://localhost:5000/main/item/-JJHkhfghsiu45ve
在我的html中,我希望能够使用* ngIf来显示某些内容(如果这不是路由).我不想为此使用路由器插座.
In my html I want to be able to use an *ngIf to show something if this is the route or not. I do not want to use router outlet for this.
我遇到的问题可能与参数有关.
The issue I am having might be regarding the param.
我正在做类似的事情:
<div *ngIf="this.router.url !== '/main/item:id'">
但这对我不起作用,因为它始终认为这是错误的.谢谢
But this does not work for me as it sees this as always false. Thanks
推荐答案
我要做的是将您的 * ngIf
设置为布尔值.然后使用ngOninIt或类似的方法来更改布尔值,具体取决于您的网址.这是一个有效的 plunker .注意:仅关注插件中的home.ts文件.您还可以根据需要更改 * ngIf
值,以证明这是可行的.
What I would do is set your *ngIf
equal to a boolean value. then use ngOninIt or something along those lines to change the value of that bool depending on your Url. Here is a working plunker. NOTE: only focus on the home.ts file in the plunker. you can also prove that this is working by changing the *ngIf
value as necessary.
P.S.对不起,塞车者到处都是无关紧要的信息,尽管这是重要的事情...
P.S sorry the plunker is filled with irrelevant info here is the important stuff though...
import { Component } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
@Component({
template: `<h1>home</h1> <button (click)="Nav()">nav to second</button>
<div *ngIf="!mybool">I am only showing if my route is home!</div>
`,
styleUrls:['style.css'],
})
export class HomeComponent {
mybool:boolean;
constructor(private router: Router,){
console.log(this.router.url);
}
ngOnInit(
showDiv(){
if(this.router.url === '/'){
this.mybool=false;
}
}
)
这篇关于Angular2-* ng如果路线是某个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!