我正在使用 angular 4 Form 和 Form-Group 组件来创建添加/编辑数据表单。我能够在添加任何新数据时进行适当的验证,但在编辑具有已填充输入字段的相同表单时遇到问题。
下面的代码是组件部分:
this.saveNotification = this.formBuilder.group({
id: '',
name: ['', Validators.required],
question: ['', Validators.required],
repeatTypesControl: ['', Validators.required],
remindAt: '',
});
if(this.isUpdateCheck){///pre-filled form on Edit
this.saveNotification.patchValue({
id: 4,
name: "Deck",
question: "Who's Deck is this?",
remindAt: "08:30pm",
});
this.saveNotification.updateValueAndValidity();
for (var i in this.saveNotification.controls) {
this.saveNotification.controls[i].markAsTouched();
}
下面的代码是模板部分:
<button ion-button color="secondary" type="submit" [disabled]="(!saveNotification.valid)" color="dark">SAVE</button>
我总是将 saveNotification.valid 值设为 false,因为在编辑时,这些值是从组件中逐个填充的。我试过 this.saveNotification.controls[i].markAsTouched() 但这不起作用。