无法从对话框访问 Ionic/Angular 中 "this" 的父引用
Can't access parent reference for "this" in Ionic/Angular from dialog
我有一个 Ionic 页面(我称之为页面),它有一个打开对话框的按钮。该对话框被定义为单独文件中的单独页面(.ts、.module.ts、.scss、.html)。
在对话框中有一个按钮,单击该按钮会执行 AJAX 请求,成功后我需要更改父 Ionic 页面中的数据。这似乎行不通。
这是我在Page中打开对话框并将回调函数传递给对话框的方式:
openReset(){
const resetModal : Modal = this.modal.create(DeploymentConfigurationResetPage, { car: this.car, callback: (car) => this.hardResetCallback(car) });
resetModal.present();
}
这是页面中定义的回调函数:
hardResetCallback(car:Car){
this.car=car;
this.ref.detectChanges();
}
在对话框的 .ts 文件中,我通过导航参数得到了汽车和回调:
ionViewDidLoad() {
this.deployment=this.navParams.get('car');
this.callback=this.navParams.get('callback');
}
在对话框中我有一个触发此功能的按钮,它应该从服务器获取重置汽车并将当前汽车替换为服务器返回的汽车。如您所见 - 服务调用成功方法调用回调:
hardReset(){
this.loading.show();
this.serviceX.resetCarConfiguration(this.car).subscribe(
(response: any) => {
if (response) {
this.message.showToast({ message: this.carReset });
}
this.callback(response);
this.closeModal();
},
(error: Error) => {
console.error(error);
this.loading.hide();
this.message.showToast({
message: this.carResetError
});
},
() => {
this.loading.hide();
}
);
}
但是页面中的汽车并没有改变。我什至在回调函数中使用了 ChangeDetectorRef (this.ref),但它并没有改变。
当我在回调函数中 console.log "this" 时,它似乎显示了正确页面的上下文,只是不知何故它似乎是页面的不同实例?
这里的问题不是对话框,而是当 this.car 未绑定到视图时我希望视图发生变化...有一个名为
的变量
this.carSection = this.car.sections[iSectionIndex]
那是必看的。
因此,如果有人遇到此问题...请仔细检查您的绑定。
我有一个 Ionic 页面(我称之为页面),它有一个打开对话框的按钮。该对话框被定义为单独文件中的单独页面(.ts、.module.ts、.scss、.html)。
在对话框中有一个按钮,单击该按钮会执行 AJAX 请求,成功后我需要更改父 Ionic 页面中的数据。这似乎行不通。
这是我在Page中打开对话框并将回调函数传递给对话框的方式:
openReset(){
const resetModal : Modal = this.modal.create(DeploymentConfigurationResetPage, { car: this.car, callback: (car) => this.hardResetCallback(car) });
resetModal.present();
}
这是页面中定义的回调函数:
hardResetCallback(car:Car){
this.car=car;
this.ref.detectChanges();
}
在对话框的 .ts 文件中,我通过导航参数得到了汽车和回调:
ionViewDidLoad() {
this.deployment=this.navParams.get('car');
this.callback=this.navParams.get('callback');
}
在对话框中我有一个触发此功能的按钮,它应该从服务器获取重置汽车并将当前汽车替换为服务器返回的汽车。如您所见 - 服务调用成功方法调用回调:
hardReset(){
this.loading.show();
this.serviceX.resetCarConfiguration(this.car).subscribe(
(response: any) => {
if (response) {
this.message.showToast({ message: this.carReset });
}
this.callback(response);
this.closeModal();
},
(error: Error) => {
console.error(error);
this.loading.hide();
this.message.showToast({
message: this.carResetError
});
},
() => {
this.loading.hide();
}
);
}
但是页面中的汽车并没有改变。我什至在回调函数中使用了 ChangeDetectorRef (this.ref),但它并没有改变。
当我在回调函数中 console.log "this" 时,它似乎显示了正确页面的上下文,只是不知何故它似乎是页面的不同实例?
这里的问题不是对话框,而是当 this.car 未绑定到视图时我希望视图发生变化...有一个名为
的变量 this.carSection = this.car.sections[iSectionIndex]
那是必看的。
因此,如果有人遇到此问题...请仔细检查您的绑定。