Let say I have this component with the following template
<div *ngFor="let t of test">{{t}}</div>
And this code
test: number[] = [1, 2, 3];
ngOnInit() {
this.test = this.test.reduce((a, b) => {
a.push(b * 10);
return a;
}, []);
setTimeout(() => {
this.test.push(4);
}, 3000);
}
This will be result in this
10
20
30
4 // not what I was looking for
However if I decide to move the code in a method getTest()
<div *ngFor="let t of getTest()">{{t}}</div>
with the code
getTest(): number[] {
return this.test.reduce((a, b) => {
a.push(b * 10);
return a;
}, []);
}
Then the delayed value will show as 40 which is what I was looking for.
Is this a valid implementation or is resource consuming? getTest() method seems to be called quite often.
In a bigger picture I'm trying to add / remove / update items in an array and show a reduced version of that array on screen.