【发布时间】:2018-09-17 16:41:48
【问题描述】:
我对这个错误感到困惑,我有 ExpressionChangedAfterItHasBeenCheckedError 错误,我试图理解 ngAfterContentInit 但我不确定如何在我的情况下使用它,如果有人可以让我知道为什么会出现这个问题发生。 _state 用于防止循环调用。
private password_disabled: any;
private sudo: any;
private locked: any;
private password_disabled_state = false;
private sudo_state = false;
private locked_state = false;
constructor(.....) {}
afterInit(entityForm: any) {
this.isNew = entityForm.isNew;
this.password_disabled = entityForm.formGroup.controls['password_disabled'];
this.sudo = entityForm.formGroup.controls['sudo'];
this.locked = entityForm.formGroup.controls['locked'];
this.password_disabled.valueChanges.subscribe((password_disabled)=>{
if(password_disabled && !this.sudo_state && !this.locked_state){
entityForm.setDisabled('sudo', password_disabled);
entityForm.setDisabled('locked', password_disabled);
this.password_disabled_state = password_disabled;
} else {
this.password_disabled_state = !this.password_disabled_state;
entityForm.setDisabled('sudo', !this.sudo_state);
entityForm.setDisabled('locked', !this.locked_state);
}
})
this.locked.valueChanges.subscribe((locked)=>{
if(locked && !this.password_disabled_state){
entityForm.setDisabled('password_disabled', locked);
this.locked_state = locked;
}
else{
this.locked_state = !this.locked_state;
}
});
this.sudo.valueChanges.subscribe((sudo)=>{
if(sudo && !this.password_disabled_state){
entityForm.setDisabled('password_disabled', sudo);
this.sudo_state = sudo;
}
else{
this.sudo_state = !this.sudo_state;
}
});
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'id: true'. Current value: 'id: false'.
【问题讨论】:
-
所有这些标志的存在是一种严重的设计气味。您可能会遇到各种问题,您的逻辑将无法维护
-
嗯,好的,我同意,但这就是我目前所拥有的。如果你愿意,建议我一个更好的方法:)
-
嗯...将内容存储在表单字段和组件类的属性中是个坏主意
标签: angular typescript