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();
}
它说 "Can't read property 'detectChanges' of undefined"。我可能缺少什么?
编辑:
回调是从模态调用的。模态通过导航参数获取回调 - 在我调用的父组件中:
const resetModal : Modal = this.modal.create('CarConfigurationResetPage', { car: this.car, callback: this.hardResetCallback });
resetModal.present();
然后这就是我在模态中获取它的方式:
this.callback=this.navParams.get('callback');
我在 AJAX 调用的成功方法中从模态调用回调,如下所示:
this.callback(response);
hardResetCallback = (car:Car) => {
this.car=car;
this.ref.detectChanges();
}
使用粗箭头函数来防止在 hardResetCallback 方法的范围内创建“this”。
查看更多关于箭头函数的信息here.
相关引用:
"In classic function expressions, the this keyword is bound to
different values based on the context in which it is called. With
arrow functions however, this is lexically bound. It means that it
uses this from the code that contains the arrow function."
您可以使用:
this.ref.markForCheck();
if (!this.ref['destroyed']) {
this.ref.detectChanges();
}
我正在像这样导入 ChangeDetectorRef:
import { Component, ViewChild, ChangeDetectorRef , ElementRef } from '@angular/core';
并在我的页面的构造函数中初始化一个变化检测器,如下所示:
constructor(
...
private ref: ChangeDetectorRef
)
但是当我在回调函数中执行 detectChanges() 时:
hardResetCallback(car:Car){
this.car=car;
this.ref.detectChanges();
}
它说 "Can't read property 'detectChanges' of undefined"。我可能缺少什么?
编辑:
回调是从模态调用的。模态通过导航参数获取回调 - 在我调用的父组件中:
const resetModal : Modal = this.modal.create('CarConfigurationResetPage', { car: this.car, callback: this.hardResetCallback });
resetModal.present();
然后这就是我在模态中获取它的方式:
this.callback=this.navParams.get('callback');
我在 AJAX 调用的成功方法中从模态调用回调,如下所示:
this.callback(response);
hardResetCallback = (car:Car) => {
this.car=car;
this.ref.detectChanges();
}
使用粗箭头函数来防止在 hardResetCallback 方法的范围内创建“this”。
查看更多关于箭头函数的信息here.
相关引用:
"In classic function expressions, the this keyword is bound to different values based on the context in which it is called. With arrow functions however, this is lexically bound. It means that it uses this from the code that contains the arrow function."
您可以使用:
this.ref.markForCheck();
if (!this.ref['destroyed']) {
this.ref.detectChanges();
}