【问题标题】:ngFor while string is "something" is posible?ngFor while string is "something" 是可能的吗?
【发布时间】:2018-12-01 17:43:15
【问题描述】:

在这种情况下,仅当 id 为 1 时,我才需要在 ngfor 中显示列表的一部分。 我怎样才能做到这一点?

while id==1

*ngFor="let item of list">{{item.value}}

this.list = [
      {id:1, value:1},
      {id:1, value:2},
      {id:1, value:3},
      {id:2, value:4},
      {id:2, value:5},
      {id:2, value:6}
  ];

【问题讨论】:

  • this.filteredList = this.list.filter(e => e.id === 1);,现在你使用 ngFor 来显示过滤列表的每个元素
  • 请添加您已经尝试过的任何代码/方法。

标签: angular ngfor


【解决方案1】:

您可以在*ngFor 语句中使用*ngIf 语句。比如:

<div *ngFor='let item of list'>
    <div *ngIf='item.id==1'>
        {{item.value}}
    </div>
</div>

这是一个有效的 stackblitz 示例 https://stackblitz.com/edit/angular-skf1wq

【讨论】:

    【解决方案2】:

    很简单,只需要在HTML中执行ngFor时检查data.id == 1`

    <div *ngFor="let data of list">
        // Hear you have to check with *ngIf
        <div *ngIf="data.id == 1">
            {{data.value}}          // It will print only when data id is 1
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多