【发布时间】:2020-08-24 04:45:27
【问题描述】:
在下面的例子中,
- 第一个表达式
{{ bar }}永远不会更新,但是 - 第二个表达式
{{ "" + bar }}已更新:
例如
two
1588950994873
为什么会这样?
import { Component } from "@angular/core";
@Component({
selector: "my-app",
template: `
What is different and why?
<br />
{{ bar }} <br />
{{ "" + bar }} <br />
`
})
export class AppComponent {
value: string = "default";
bar: any = d => {
this.value = d;
};
ngOnInit() {
this.bar.valueOf = () => this.value;
this.bar("one");
setInterval(() => this.bar(Date.now() + ""), 1000);
this.bar("two");
}
}
【问题讨论】:
-
只是一个理论(对这种行为没有任何启发):当它只需要取消引用一个变量并且必须计算它将显示在花括号内的值时,Angular 处理不同的简单表达式.
标签: angular typescript asynchronous angular9 zonejs