2017-12-29 23 views
0

これは簡単なAngular4アプリケーションです。 私は補間を使ってメソッドを呼び出しています。それは4回実行されています。補間式が何度も実行されるのはなぜですか?

app.component.ts

import { Component} from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 1; 



testing(){ 
    console.log('IN testing 0'); 

} 

private testing1(){ 
    console.log('IN testing 1'); 
} 
} 

app.component.htmlページは4回 "テスト1の" それプリントをロードされている

<h2>{{testing1()}}</h2> 
<button (click)="testing()">Testing2 </button> 

。ボタンをクリックすると、 "IN testing 1"が2回印刷されます。私はその流れを理解することができません。

+0

ダイジェストサイクルの理解に役立つ[スレッド](https://stackoverflow.com/questions/24698620/dirty-checking-on-angular)です。 – alphapilgrim

答えて

0

このような関数を呼び出すと、<h2>{{testing1()}}</h2>の各変更検出でtesting1()関数がトリガされます。その理由は、その印刷コンソールは複数の時間です。これは悪い習慣です。関数を使用する代わりに、変数を使用してください

関連する問題