Angular2 - * ngIf路线是一个特定的参数

时间:2016-11-01 14:21:21

标签: angular

我有一个路线/主要/项目:id

看起来像:

htmlspecialchars()

在我的html中,我希望能够使用* ngIf显示某些内容,如果这是路线。我不想为此使用路由器插座。

我遇到的问题可能是关于参数。

我正在做类似的事情:

http://localhost:5000/main/item/-JJHkhfghsiu45ve

但这对我不起作用,因为它认为这总是错误的。 感谢

1 个答案:

答案 0 :(得分:3)

我要做的是将*ngIf设置为等于布尔值。然后使用ngOninIt或这些行中的某些内容来根据您的Url更改该bool的值。这是一个有效的plunker。注意:只关注plunker中的home.ts文件。您还可以根据需要更改*ngIf值来证明这是有效的。

对不起,对于那些不相关的信息来说,这里的搜索是重要的事情,但是......

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;
  }
}
)