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')
}
为什么最后两个不表现相同?它们不应该是同一件事吗?
Ionic版本:3.9.2
Angular版本: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()
}