【发布时间】:2017-02-09 16:50:50
【问题描述】:
我正在通过patchValue 从我的组件更新反应式FormGroup 控件值。
例如:
this.myForm.patchValue({incidentDate:'2016-09-12');
这很好用并触发valueChanges 事件,但是此控件的dirty 属性仍然是false。
我需要将incidentDate 控件设为dirty,以便我的验证逻辑知道要针对此控件运行。
如何从我的组件更新控件的值并指出它是脏的?
这是我的验证逻辑:
onValueChanged(data?: any) {
if (!this.myForm) {
return;
}
const form = this.myForm;
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages: any = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
}
【问题讨论】:
标签: angular