模板中的{{call()}}多次执行方法块?

Pra*_*tha 4 lifecycle-hook angular

这里多次调用测试方法中的语句.为什么会这样?DOM是由AngularJS2多次重建的吗?

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

@Component({
  selector: 'my-app',
  template: `<div>Method Call {{test()}}</div>>`
})

export class AppComponent { 
    name = 'Angular';
    test() {
       console.log("Test is called");
    }
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 10

{{test()}} 每次Angular运行变化检测时都会进行评估,这可能是经常发生的.

不鼓励从视图绑定到函数或方法.首选将方法调用的结果赋给属性并绑定到此属性.

@Component({
  selector: 'my-app',
  template: `<div>Method Call {{someValue}}</div>>`
})

export class AppComponent { 
    ngOnInit() {
      this.test();
    }
    name = 'Angular';
    test() {
       this.someValue = "Test is called";
    }
}
Run Code Online (Sandbox Code Playgroud)