私は角度テンプレート(補間)の内側から関数を呼び出していますが、不思議なことに関数は4回呼び出されています!!なぜ4番目にテンプレートが4回コンパイルされるのですか?
バインディング値が更新された場合の変更検出プロセスのキックスタートを理解したので、変更検出がどのように機能するかは知っています。このシナリオでは
は、私のような任意のものを変更/結合ではないのです。..var i = 0;
@Component({
selector: 'my-app',
template: `
<div>
<h1>{{ parentCounter() }}</h1>
</div>
`,
})
export class App {
name:string;
constructor() {
}
parentCounter(){
alert(`${++i} called`);
return 5;
}
}
参考:https://plnkr.co/edit/pta0s0nzcsLdTsjCtb3D?p=preview
注:テンプレートは4回前にコンパイルました非同期イベント
あなたは
<h1>{{ parentCounter() }}</h1>
のようなビューの機能に特異的に結合する場合
https://stackoverflow.com/questions/45412199/angular-change-detection-runs-eight-times-instead-four/45412670#45412670 – yurzui
https://stackoverflow.com/questions/41187667/why-angular2 -executes-methods-several-times/41191641#41191641 – yurzui