有没有办法计算Angular html页面中的所有循环布尔值

时间:2018-03-23 20:12:29

标签: angular

是否有方法计算Angular html页面中的所有循环布尔值

this.items = [{
    id:1,
    isUpdate: false
  },
  {
    id:2,
    isUpdate: true
  },
  {
    id:3,
    isUpdate: true
  }

只要其中一个isUpdate值为true,无论有多少isUpdate值为true,通知组件都会显示并只显示一次。

我想知道是否有办法计算app.component.html页面中的布尔值,看看是否至少有一个isUpdate值为true并显示通知组件。而且我不想多次显示通知组件,只显示一次。

到目前为止,我的代码如下所示,它会多次显示通知:

app.component.html
<div *ngFor = "let notification of items">
  <notification *ngIf="notification.isUpdate"></notification>
</div>

Notification.component.html
<alert type="warning">
  <span class="icon-notification"></span>  Ready to update.
</alert>

提前谢谢。

1 个答案:

答案 0 :(得分:5)

编写一个如果应该显示通知将返回的函数:

showNotification(): boolean {    
  this.items.forEach(item => {
    if (item.isUpdate) {
      return true;
    }
  });

  return false;
}

并将其绑定到您的模板:

<notification *ngIf="showNotification()"></notification>