gpt4 book ai didi

javascript - 单击按钮后 Ionic-Angular 无法更新 View

转载 作者:行者123 更新时间:2023-12-02 22:20:01 25 4
gpt4 key购买 nike

为什么单击 ion-alert 组件中的按钮后, View 没有更新?例如,如果我的组件中有一个名为 sending 的属性以及 ionic 警报按钮的处理程序:

  // in my component
public sending = false;

constructor(
public alertCtrl: AlertController,
) { }

async deleteFile() {
const alert = await this.alertCtrl.create({
header: 'Deseas eliminar el archivo?',
message: '',
buttons: [
{
text: 'Eliminar',
role: 'eliminar',
cssClass: 'btn-alert',
handler: () => {
this.sending = true;
}
}
]
});

await alert.present();
}

<!-- in my view -->
<div> {{ sending }} </div>

除非我在处理程序内调用 Angular 方法 ** markForCheck () **,否则 View 不会更新。

我正在使用 ionic 4 和 Angular。

英语不是我的母语,所以请原谅任何错误

最佳答案

这里查看我在代码中的注释。我希望这解释了这是预期的行为,并且有两种方法可以更新“发送”属性的值(在处理程序或 onDidDismiss Hook 内):

import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular'

@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.css'],
})
export class HomePage {

public sending = false;

constructor(
public alertCtrl: AlertController,
) { }

async deleteFile() {
const alert = await this.alertCtrl.create({
header: 'Deseas eliminar el archivo?',
message: '',
buttons: [
{
text: 'Eliminar',
role: 'eliminar',
cssClass: 'btn-alert',
handler: () => {
// this update of property "sending" happens in the "alert" component and the HomePage component does not learn about this change until next change detection cycle:
this.sending = true;
}
}
]
});
await alert.present();
alert.onDidDismiss().then(() => {
// this update will happen after "alert" dismiss and within the scope of the HomePage component.
this.sending = true;
});
}
}

玩一下: https://stackblitz.com/edit/ionic-4-template-psfsbd

关于javascript - 单击按钮后 Ionic-Angular 无法更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com