简体   繁体   English

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

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

I have an Ionic page (I'll call it Page) that has a button that opens a dialog. 我有一个Ionic页面(我将其称为Page),该页面具有一个打开对话框的按钮。 The dialog is defined as a separate page in separate files(.ts, .module.ts, .scss, .html). 对话框被定义为单独文件(.ts,.module.ts,.scss,.html)中的单独页面。

In the dialog there is a button that when clicked performs an AJAX request and on success I need to change data in the parent Ionic page. 在对话框中,有一个按钮,单击该按钮时会执行AJAX请求,成功后,我需要更改父级Ionic页面中的数据。 And this just doesn't seem to work. 而且这似乎不起作用。

This is how I open the dialog in the Page and pass the callback function to dialog: 这是我在页面中打开对话框并将回调函数传递给对话框的方式:

openReset(){
    const resetModal : Modal = this.modal.create(DeploymentConfigurationResetPage, { car: this.car, callback: (car) => this.hardResetCallback(car) });
    resetModal.present();
  }

This is the callback function defined in the Page: 这是在页面中定义的回调函数:

hardResetCallback(car:Car){
    this.car=car;
    this.ref.detectChanges();
  }

In the .ts file of the dialog I get the car and callback like this through nav params: 在对话框的.ts文件中,我通过导航参数获得了汽车和回调,如下所示:

  ionViewDidLoad() {
    this.deployment=this.navParams.get('car');
    this.callback=this.navParams.get('callback');
  }

And in the dialog I have a button that triggers this function which is supposed to get the reset car from the server and replace the current car with the one that's returned by the server. 在对话框中,我有一个按钮触发该功能,该按钮应该从服务器获取重置的汽车并将当前的汽车替换为服务器返回的汽车。 As you can see - service calls success method calls the 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();
      }
    );
  } 

But the car in the Page doesn't change. 但是Page中的汽车没有改变。 I even use the ChangeDetectorRef (this.ref) in the callback function, but it just doesn't change. 我什至在回调函数中使用ChangeDetectorRef(this.ref),但它没有改变。

When I console.log "this" in the callback function, it seems like it shows the context for the right page, it just somehow seems it's a different instance of the page? 当我在回调函数中使用console.log“ this”时,似乎显示了正确页面的上下文,只是某种程度上它似乎是该页面的另一个实例?

The problem here was not the dialog, but that I was expecting the view to change when this.car wasn't bound to view... There was a variable called 这里的问题不是对话框,而是我期望当this.car不绑定查看时,视图会发生变化。有一个名为

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

that was bound to view. 那是必然的。

So if anyone has this problem... Check your bindings carefully. 因此,如果有人遇到此问题...请仔细检查您的绑定。

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

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