[英]Uncaught Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
Uncaught Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
未捕获的错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 Previous value: 'ngIf: false'.
以前的值:'ngIf:false'。 Current value: 'ngIf:
当前值:'ngIf:
I have been trying to get updated value on clicking button after going to several sub pages with in the same component, YES and NO value does not update(Only on the current page it works), though I can see it through console log in my method.在转到同一组件中的几个子页面后,我一直试图在单击按钮时获取更新的值, YES和NO值不会更新(仅在当前页面上有效),尽管我可以通过控制台日志看到它方法。 Below is my code.
下面是我的代码。 and I tried below URL
我试过下面的网址
ngIf - Expression has changed after it was checked ngIf - 检查后表达式已更改
constructor(
) {
}
getAssessBtn1(value: any) {
this.assesseeValidation = false;
if (value == 'Yes') {
this.showAssessBtn1 = true;
this.showAssessCondition1 = false;
} else {
this.showAssessBtn1 = false;
}
}
I was trying with ngAfterViewChecked, how can I use the method getAssessBtn1(value: any)
inside it.我正在尝试使用 ngAfterViewChecked,如何在其中使用
getAssessBtn1(value: any)
方法。
HTML : HTML :
<button data-auto-id="AdultTravellersAssess" *ngIf="showAssessBtn1
class="btn btn-secondary assess-btn custom-theme-group-two"
(click)="test()">
Access
You can use ChangeDetectionRef
for triggering change detection event right after changing the showAssessBtn
or showAssesCondition1
.您可以使用
ChangeDetectionRef
改变后触发变化检测事件右showAssessBtn
或showAssesCondition1
。
// inject the changeDetectionRef into your component with
constructor(private cd: ChangeDetectionRef) {}
// in the getAssessBtn1 method trigger change detection
getAssessBtn1(value: any) {
this.assesseeValidation = false;
if (value == 'Yes') {
this.showAssessBtn1 = true;
this.showAssessCondition1 = false;
} else {
this.showAssessBtn1 = false;
}
this.cd.detectChanges();
}
With this approach, angular will not complain about expression changed after value is checked.使用这种方法,angular 不会抱怨检查值后表达式更改。 Because you are re-triggering the change detection event when you set your expression to another value.
因为当您将表达式设置为另一个值时,您正在重新触发更改检测事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.