状态改变但组件不更新 - ANGULAR
State changes but component doesn't update - ANGULAR
我将 ionic 与 angular 一起使用,当我在确定按钮的回调函数中创建警报时,我正在更改我的状态。状态发生变化,但这种变化对 iu 没有影响,我认为组件没有更新。我该如何解决这个问题?
async presentAlert() {
const alert = await this.alertController.create({
header: '',
message: '',
buttons: [
'cancel',
{
text: 'ok',
handler: () => {
this.currentScreen = "";
this.dates[this.currentDateIndex].isOrdered = false;//disable order
}
}
]
});
await alert.present();
}
您可以尝试使用 ChangeDetectorRef
来明确声明已进行更改并且需要更新视图。
参考:https://angular.io/api/core/ChangeDetectorRef
示例:
在构造函数中声明 ChangeDetectorRef
constructor(public cd: ChangeDetectorRef) {}
然后在你的回调中使用它:
buttons: [
'cancel',
{
text: 'ok',
handler: () => {
this.currentScreen = "";
this.dates[this.currentDateIndex].isOrdered = false;//disable order
this.cd.detectChanges();
}
}
]
我将 ionic 与 angular 一起使用,当我在确定按钮的回调函数中创建警报时,我正在更改我的状态。状态发生变化,但这种变化对 iu 没有影响,我认为组件没有更新。我该如何解决这个问题?
async presentAlert() {
const alert = await this.alertController.create({
header: '',
message: '',
buttons: [
'cancel',
{
text: 'ok',
handler: () => {
this.currentScreen = "";
this.dates[this.currentDateIndex].isOrdered = false;//disable order
}
}
]
});
await alert.present();
}
您可以尝试使用 ChangeDetectorRef
来明确声明已进行更改并且需要更新视图。
参考:https://angular.io/api/core/ChangeDetectorRef
示例:
在构造函数中声明 ChangeDetectorRef
constructor(public cd: ChangeDetectorRef) {}
然后在你的回调中使用它:
buttons: [
'cancel',
{
text: 'ok',
handler: () => {
this.currentScreen = "";
this.dates[this.currentDateIndex].isOrdered = false;//disable order
this.cd.detectChanges();
}
}
]