当我在Angular中使用返回数据的函数来使用*ngFor时,该函数会被多次调用,有时甚至会导致循环:
app.component.ts
export class AppComponent {
getArray(): string[] {
//here i know when this function is called
console.log('getArray called')
return ['number one', 'number two']
}
}
app.component.html
<h1 *ngFor="let item of getArray()">
{{ item }}
</h1>
我的控制台:
然后我发现函数getArray()被多次调用,但我不知道为什么。