【发布时间】:2021-09-08 04:36:56
【问题描述】:
我想知道下面这段代码显示了多少项。
component.html:
<p *ngIf="counter">{{counter}} items displayed!</p>
<p *ngIf="!counter">Nothing to display!</p>
<ng-container *ngFor="let item of items">
<li *ngIf="item.count < 100">{{counter}}. {{item.title}}</li>
</ng-container>
component.ts:
export class ListComponent {
@Input('items') items: any[];
constructor() { }
protected counter: number = 0;
}
项目(json 演示):
[
{id: 1, count: 100, title: "title #1"},
{id: 2, count: 20, title: "title #2"},
{id: 3, count: 0, title: "title #3"},
{id: 4, count: 200, title: "title #4"},
{id: 5, count: 100, title: "title #5"},
]
注意 1:在上面的示例数据中,只有数组中对象的 count 属性可以在任何时候被应用程序的任何其他组件更改。
注意 2:实际上,items 是一个数组数组,但为了更好地表示和更好地理解,我在这里将其更改为对象数组。
我试图计算 HTML 节点,但发生了这个错误:
NG0100: ExpressionChangedAfterItHasBeenCheckedError
我也尝试过 *ngIf="addCounter(item.count
我也无法过滤ts文件中的items(有很多ngfors并且items不断更新,所以ts文件因为一个简单的计数器而变得过于复杂)。
有没有更好的方法?
【问题讨论】:
标签: angular