Angular2:如何通过`patchValue()`将FormGroup控件标记为脏 [英] Angular2: How do you mark FormGroup control dirty via `patchValue()`
本文介绍了Angular2:如何通过`patchValue()`将FormGroup控件标记为脏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过例如:
this.myForm.patchValue({incidentDate:'2016-09-12');
这很好用并触发了valueChanges
事件,但是此控件的dirty
属性仍然是false
.
This works great and triggers a valueChanges
event, however this control's dirty
property is still false
.
我需要incidentDate
控件是dirty
,这样我的验证逻辑才能针对此控件运行.
I need the incidentDate
control to be dirty
so my validation logic knows to run against this control.
如何从组件中更新控件的值并指示其脏了?
How do I update the value of a control from my component AND indicate that it is 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] + ' ';
}
}
}
}
推荐答案
我通常这样做:
this.formControl.markAsDirty()
或者您的情况可能是:
this.myForm.get('incidentDate').markAsDirty()
这篇关于Angular2:如何通过`patchValue()`将FormGroup控件标记为脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文