* ngIf用于Angular2中的html属性

马克·蒂莫西(Mark Timothy):
<ion-navbar  hideBackButton >
  <ion-title> </ion-title>
  ...
  ...

我想hideBackButton有条件地到达那里,并且我不想使用* ngIf重复整个ion-navbar元素。是否可以将* ngIf应用于hideBackButton属性?

蒂埃里·坦维尔(Thierry Templier):

您可以利用插值:

<ion-navbar [attr.hideBackButton]="someExpression">
  <ion-title> </ion-title>
  ...
...

如果someExpression为null,则属性将不存在;如果someExpression为空字符串,则属性将存在。这是一个示例:

@Component({
  selector: 'my-app',
  template: `
    <div [attr.hideBackButton]="someExpression">
      Test
    </div>
    <div (click)="toggleAttribute()">Toggle</div>
  `
})
export class AppComponent {
  constructor() {
    this.someExpression = null;
  }

  toggleAttribute() {
    if (this.someExpression==null) {
      this.someExpression = '';
    } else {
      this.someExpression = null;
    }
  }
}

看到这个plunkr:https ://plnkr.co/edit/LL012UVBZ421iPX4H59p ? p = preview

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章