【发布时间】:2018-08-23 08:37:10
【问题描述】:
如果在 await 语句之后更改了数据绑定的值,则不会更新数据绑定。
handle() {
this.message = 'Works'
}
async handle() {
this.message = 'Works'
}
async handle() {
await new Promise((resolve, reject) => {
resolve()
})
this.message = 'Works'
}
async handle() {
await new Promise((resolve, reject) => {
setTimeout(() => resolve(), 3000)
})
this.message = 'Doesn\'t work'
}
handle() {
new Promise((resolve, reject) => {
setTimeout(() => resolve(), 3000)
})
.then(() => this.message = 'Works')
}
为什么最后两个行为不一样?他们不应该是同一件事吗?
离子:3.9.2
角度:5.0.3
TypeScript:2.4.2
编辑:我遇到了另一个可能对某些人有用的问题。
在构造函数中更改绑定的值与 ionViewDidLoad 或 ngOnInit 的行为不同!
constructor(private zone: NgZone) {
// This will cause the same problems, bindings not updating
this.handle()
}
constructor(private zone: NgZone) {
// Unless you do this...
this.zone.run(() => {
this.handle()
})
}
ionViewDidLoad() {
// But I think this is better/cleaner
this.handle()
}
【问题讨论】:
-
@Jeto 它适用于console.log,问题是如果您在具有数据绑定的组件中尝试它,则值不会更新
-
那你能用它构建一个最小的 StackBlitz 吗?
-
我在试,之前没听说过:)
-
我似乎无法在那里重现它...我的包是不同的版本,我也无法设置 tsconfig。
标签: javascript angular typescript data-binding async-await