angular - ExpressionChangedAfterItHasBeenCheckedError 从父更新子表单的验证器
问题描述
我已经创建了一些反应性子表单,我已经使用 连接到父表单组FormGroupDirective
,但是当我在添加控件时从父表单组更新子表单验证器ngAfterViewInit
并调用时,updateValueAndValidity
我得到了这个错误控制台:
ERROR
Error: ExpressionChangedAfterItHasBeenCheckedError:
Expression has changed after it was checked.
Previous value: 'ng-valid: true'. Current value: 'ng-valid: false'.
有没有可能避免这个错误?我创建了该问题的StackBlitz,当我设置配置文件描述验证器并调用updateValueAndValidity
.ngAfterViewInit
PageComponent
解决方案
您可以在您的钩子中运行更改检测:
import { ChangeDetectorRef } from '@angular/core';
constructor(private changeDetector: ChangeDetectorRef) { }
public ngAfterViewInit() {
...
this.changeDetector.detectChanges();
}
或者使用 onPush 变化检测:
import { ..., ChangeDetectionStrategy } from '@angular/core';
@Component({
...
changeDetection: ChangeDetectionStrategy.OnPush
})
这将确保更改检测仅通过比较引用而不是在变异时运行。
推荐阅读
- javascript - NestJS:如何在中间件中获取 ExecutionContext
- sql-server - 并发处理存储过程
- android - 如何在预览中选择材质主题?
- dns - 无法使用 bitnami.conf 运行多个子域
- c# - C# 如何使用反射从没有 IConvertible 的类型进行转换
- c# - 无法使用 BotFramework 4 显示简单的 ReceiptCard
- javascript - Javascript Canvas 在没有 requestAnimFrame 的情况下创建心跳
- android - CoordinatorLayout 返回 null
- swift - 选择器视图未显示数组数据
- python - 用两个条件替换重复项