簡體   English   中英

Ionic,Angular - ChangeDetectorRef未定義

[英]Ionic, Angular - ChangeDetectorRef is undefined

我正在導入ChangeDetectorRef,如下所示:

import { Component, ViewChild, ChangeDetectorRef , ElementRef } from '@angular/core';

並在我的頁面的構造函數中初始化更改檢測器,如下所示:

constructor(
    ...
    private ref: ChangeDetectorRef
  )

但是當我在回調函數中執行detectChanges()時:

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

它說“無法讀取屬性'detectChanges'未定義”。 我能錯過什么?

編輯:

從模態調用回調。 模態通過nav params獲取回調 - 在我調用的父組件中:

const resetModal : Modal = this.modal.create('CarConfigurationResetPage', { car: this.car, callback: this.hardResetCallback });
    resetModal.present();

然后這就是我在模態中得到它的方式:

 this.callback=this.navParams.get('callback');

我在AJAX調用的success方法中調用模態回調,如下所示:

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

使用胖箭頭函數可以防止在hardResetCallback方法的范圍內創建“this”。

在此處查看更多有關箭頭功能

相關報價:

“在經典函數表達式中,this關鍵字根據調用它的上下文綁定到不同的值。但是,使用箭頭函數,它是詞法綁定的。這意味着它使用包含箭頭函數的代碼中的這個。”

您可以使用:

this.ref.markForCheck();
if (!this.ref['destroyed']) {
    this.ref.detectChanges();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM