讓說我有以下模板角裝訂方法或變量
<div *ngFor="let t of test">{{t}}</div>
這個組件,而此代碼
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);
}
這將導致該
10
20
30
4 // not what I was looking for
但是,如果我決定在方法中移動代碼getTest()
<div *ngFor="let t of getTest()">{{t}}</div>
與代碼
getTest(): number[] {
return this.test.reduce((a, b) => {
a.push(b * 10);
return a;
}, []);
}
然後延遲值將顯示爲40
這正是我一直在尋找。
這是一個有效的實施或資源消耗?似乎經常調用方法getTest()
。
在更大的圖片中,我試圖添加/刪除/更新數組中的項目,並在屏幕上顯示該數組的縮小版本。
當然,這將是相當經常被稱爲 – yurzui