繁体   English   中英

无法从对话框访问Ionic / Angular中“ this”的父级引用

[英]Can't access parent reference for “this” in Ionic/Angular from dialog

我有一个Ionic页面(我将其称为Page),该页面具有一个打开对话框的按钮。 对话框被定义为单独文件(.ts,.module.ts,.scss,.html)中的单独页面。

在对话框中,有一个按钮,单击该按钮时会执行AJAX请求,成功后,我需要更改父级Ionic页面中的数据。 而且这似乎不起作用。

这是我在页面中打开对话框并将回调函数传递给对话框的方式:

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();
      }
    );
  } 

但是Page中的汽车没有改变。 我什至在回调函数中使用ChangeDetectorRef(this.ref),但它没有改变。

当我在回调函数中使用console.log“ this”时,似乎显示了正确页面的上下文,只是某种程度上它似乎是该页面的另一个实例?

这里的问题不是对话框,而是我期望当this.car不绑定查看时,视图会发生变化。有一个名为

 this.carSection = this.car.sections[iSectionIndex] 

那是必然的。

因此,如果有人遇到此问题...请仔细检查您的绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM