【发布时间】:2023-04-02 02:08:01
【问题描述】:
我正在使用以下代码将组件添加到 FormGroup:
for (let index = 0; index < this.options.length; index++) {
let element = 'radio_element_' + index;
this.formGroup.addControl(element, new FormControl(null, [Validators.required]));
}
循环中的每次迭代都会为一组单选按钮添加一个选项。但是,我收到以下错误:
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)
当我通过添加 null, [Validators.required] 向 FormControls 引入验证时,错误开始出现。问题的原因可能是什么?
【问题讨论】:
标签: angular validation angular-reactive-forms form-control