简体   繁体   English

Angular 8 ExpressionChangedAfterItHasBeenCheckedError 反应形式的无线电验证

[英]Angular 8 ExpressionChangedAfterItHasBeenCheckedError radio validation on reactive form

I'm using the following code to add components to a FormGroup:我正在使用以下代码将组件添加到 FormGroup:

for (let index = 0; index < this.options.length; index++) {
    let element = 'radio_element_' + index;
    this.formGroup.addControl(element, new FormControl(null, [Validators.required]));
}

Each iteration through the loop adds an option to a set of radio buttons.循环中的每次迭代都会为一组单选按钮添加一个选项。 But, I get the following error:但是,我收到以下错误:

core.js:6185 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'ng-valid': 'true'. Current value: 'false'.
    at throwErrorIfNoChangesMode (core.js:8092)
    at bindingUpdated (core.js:19773)
    at checkStylingProperty (core.js:23355)
    at ɵɵclassProp (core.js:23255)
    at NgControlStatusGroup_HostBindings (forms.js:1015)
    at setHostBindingsByExecutingExpandoInstructions (core.js:11476)
    at refreshView (core.js:11843)
    at refreshDynamicEmbeddedViews (core.js:13154)
    at refreshView (core.js:11819)
    at refreshDynamicEmbeddedViews (core.js:13154)

The error began occuring when I introduced validation to the FormControls by adding null, [Validators.required] .当我通过添加null, [Validators.required]向 FormControls 引入验证时,错误开始发生。 What could be the cause of the issue?问题的原因可能是什么?

I had the same problem and I get rid of it by removing formGroup selector from the template我有同样的问题,我通过从模板中删除 formGroup 选择器来摆脱它

instead of代替

<div [formGroup]="group">
  <input formControlName="item" />
</div>

use利用

<div>
  <input [formControl]="group.controls.item" />
</div>

this helps me, maybe it is your case, too这对我有帮助,也许你的情况也是如此

I also faced this problem.我也遇到过这个问题。 I was getting problem in directive's @Hosting property.我在指令的 @Hosting 属性中遇到问题。 I checked thoroughly where that directive was using and then in one component I have seen inside the ngOnInit() observers have been subscribed, where MarkForCheck() used.我彻底检查了该指令的使用位置,然后在一个组件中我看到 ngOnInit() 观察者已被订阅,其中使用了 MarkForCheck()。 So I just replaced the markForCheck() with detectChanges(), and finally it worked for me.所以我只是用detectChanges()替换了markForCheck(),最后它对我有用。

simply use change detector on value change event只需在值更改事件上使用更改检测器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM