【发布时间】:2019-06-25 21:27:11
【问题描述】:
在 Angular 中,有一个内置的动作对话框框架,当 showDialog = true 时,我必须使用它来显示动作弹出窗口。
当 showDialog=true 时会显示动作对话框, 否则当 showDialog=false 时操作对话框将隐藏
当showDialog = false时,必须使用setter和getter方法调用cancel方法。
但是,在调试代码时,即使我没有触发操作对话框,它也会继续检查 getter 和 setter 方法。
有没有一种方法可以在 Angular 中使用生命周期方法来比较 showDialog 的先前值和当前布尔值。示例:如果 previousValue.showDialog != currentValue.showDialog,则只调用 get showDialog()、set showDialog() 和 cancel()。
我正在考虑在 Angular 中使用一些生命周期方法,例如 ngAfterViewInit()。你知道我如何存储以前的 showDialog 布尔值,以便我可以与 showDialog 的当前值进行比较。
在 React 中,我可以使用具有 prev props 值的 getDerivedStateFromProps,但你知道 Angular 中是否有类似的方法。
a.html
<action-dialog [(openDialog)]="showDialog"/>
b.html
<button (click)="showDialog = true">
{{intl.cleanNow | uppercase}}
</button>
a.ts
get showDialog() {
return this._showDialog;
}
set showDialog(val){
if (val === false) {
this.cancel();
} else if (val === true) {
this._showDialog = true;
}
}
cancel() {
this.showDialog = false;
this.form.reset({
throttle: this._originalThrottle || 50
});
}
【问题讨论】:
标签: angular getter-setter angular-lifecycle-hooks