{{ call() }} in template executes the method block multiple times?

前端 未结 1 519
一向
一向 2020-12-20 21:43

Here the statements in test method is called multiple times. Why is this happening? Is DOM is recreated by AngularJS2 multiple times?

import { Component } fr         


        
相关标签:
1条回答
  • 2020-12-20 22:40

    {{test()}} is evaluated every time Angular runs change detection, which can be quite often.

    Binding to function or methods from the view is discouraged. Prefer assigning the result of the method call to a property and bind to this property instead.

    @Component({
      selector: 'my-app',
      template: `<div>Method Call {{someValue}}</div>>`
    })
    
    export class AppComponent { 
        ngOnInit() {
          this.test();
        }
        name = 'Angular';
        test() {
           this.someValue = "Test is called";
        }
    }
    
    0 讨论(0)
提交回复
热议问题