首页 > 解决方案 > 修补项目不会更新 Angular 中的有效性

问题描述

我有一个问题,因为在我提交表单后,即使有一个值,“必填字段”也不会消失。它应该消失。我的有效性有问题吗?请看这个链接看这个链接

TS

patchValues(id, i) {
let x = (<FormArray>this.addForm.controls['rows']).at(i);

const selectedIngredient = this.ingredients.find(y => y.id == id);

x.patchValue({
  unit_price: selectedIngredient.price
});

}

标签: angularangular6angular-reactive-formsangular-formsangular4-forms

解决方案


在这些情况下,您必须使用(例如)触发有效性检查:

x.patchValue({
  unit_price: selectedIngredient.price
});
x.get('unit_price').markAsTouched();

修补值时,不会执行验证器。

工作小提琴


推荐阅读