【问题标题】:Can't access parent reference for "this" in Ionic/Angular from dialog无法从对话框中访问 Ionic/Angular 中“this”的父引用
【发布时间】:2018-12-27 16:17:56
【问题描述】:

我有一个 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();
  }

这是Page中定义的回调函数:

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" 时,它似乎显示了正确页面的上下文,它只是以某种方式似乎是页面的不同实例?

【问题讨论】:

    标签: angular typescript ionic-framework ionic2 ionic3


    【解决方案1】:

    这里的问题不是对话框,而是当 this.car 没有绑​​定到视图时,我期望视图会改变...有一个名为

    的变量
     this.carSection = this.car.sections[iSectionIndex] 
    

    一定要查看的。

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

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2016-10-07
      • 2023-04-08
      相关资源
      最近更新 更多