I want to know how many items are displayed in following piece of code.
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;
}
items (json presentation):
[
{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"},
]
Note 1: In above sample data, only count
property of objects in the array can change at any moment of time, by any other component of the app.
Note 2: Actually, items
is an array of arrays, but for better representation and better understanding I changed it to array of objects here.
I tried to count HTML nodes but this error happens:
NG0100: ExpressionChangedAfterItHasBeenCheckedError
I also tried *ngIf="addCounter(item.count < 100)" but addCounter() is triggered on every event on page (scroll events, etc).
I also can not filter items
in the ts file (there are lots of ngfors and the items
is constantly updated, so the ts file gets too complicated because of just a simple counter).
Is there a better approach out there?