模板中的{{call()}}是否多次执行方法块? [英] {{ call() }} in template executes the method block multiple times?

查看:101
本文介绍了模板中的{{call()}}是否多次执行方法块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,test方法中的语句被多次调用.为什么会这样呢? AngularJS2是否多次创建DOM?

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

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");
    }
}

每次Angular运行更改检测时,都会评估

推荐答案

{{test()}}.

不建议从视图绑定到函数或方法.最好将方法调用的结果分配给一个属性,然后绑定到该属性.

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";
    }
}

这篇关于模板中的{{call()}}是否多次执行方法块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆