Angular formArray 中的 formGroup,formGroup 无效,但 `formArray.valid` 为真
Angular formGroup inside formArray, formGroup is invalid, but `formArray.valid` is true
这是我的主要代码:
get emailFormArray() {
return this.formGroup.get("emails") as FormArray;
}
public ngOnInit() {
this.formGroup = this.formBuilder.group({
emails: this.formBuilder.array([]),
});
this.addEmailFormGroup();
}
public addEmailFormGroup() {
this.emailFormArray.controls.push(
this.formBuilder.group({
email: ['', Validators.email],
}),
);
}
在我的模板中
{{ formGroup.valid }}
{{ emailFormArray.valid }}
<ng-container *ngFor="let email of emailFormArray.controls">
{{ email.valid }}
{{ email.get('email').valid }}
</ng-container>
当我输入无效的电子邮件时,模板中的结果是true true false false
,为什么formGroup
和emailFormArray
是有效的?谢谢!
找到原因了。
我应该使用 this.emailFormArray.push
而不是 this.emailFormArray.controls.push
我在从数据库加载数据和更新数据时遇到了同样的问题,它没有保留验证规则。我设置direct value to form control as
的问题
loadSlideItemFromDB(slideShowItem): FormArray {
const formArray = new FormArray([]);
slideShowItem.forEach(item => {
formArray.push(this.fb.group({
id: item.Id,
slideShowId: item.SlideShowId,
pageType: item.PageType,
linkUrl: item.LinkUrl,
pageTitle: item.PageTitle
}))
});
return formArray;
}
重构代码后,我先创建带有验证规则的表单数组,然后 patchValue
为它创建。
分享给关心的人
loadSlideItemFromDB(slideShowItem): FormArray {
const formArray = new FormArray([]);
slideShowItem.forEach(item => {
let group = this.fb.group({
id: [''],
slideShowId: [''],
pageType: ['1', Validators.required],
linkUrl: ['', Validators.required],
pageTitle: ['']
})
group.patchValue({
id: item.Id,
slideShowId: item.SlideShowId,
pageType: item.PageType,
linkUrl: item.LinkUrl,
pageTitle: item.PageTitle
});
formArray.push(group);
});
return formArray;
}
这是我的主要代码:
get emailFormArray() {
return this.formGroup.get("emails") as FormArray;
}
public ngOnInit() {
this.formGroup = this.formBuilder.group({
emails: this.formBuilder.array([]),
});
this.addEmailFormGroup();
}
public addEmailFormGroup() {
this.emailFormArray.controls.push(
this.formBuilder.group({
email: ['', Validators.email],
}),
);
}
在我的模板中
{{ formGroup.valid }}
{{ emailFormArray.valid }}
<ng-container *ngFor="let email of emailFormArray.controls">
{{ email.valid }}
{{ email.get('email').valid }}
</ng-container>
当我输入无效的电子邮件时,模板中的结果是true true false false
,为什么formGroup
和emailFormArray
是有效的?谢谢!
找到原因了。
我应该使用 this.emailFormArray.push
而不是 this.emailFormArray.controls.push
我在从数据库加载数据和更新数据时遇到了同样的问题,它没有保留验证规则。我设置direct value to form control as
的问题loadSlideItemFromDB(slideShowItem): FormArray {
const formArray = new FormArray([]);
slideShowItem.forEach(item => {
formArray.push(this.fb.group({
id: item.Id,
slideShowId: item.SlideShowId,
pageType: item.PageType,
linkUrl: item.LinkUrl,
pageTitle: item.PageTitle
}))
});
return formArray;
}
重构代码后,我先创建带有验证规则的表单数组,然后 patchValue
为它创建。
分享给关心的人
loadSlideItemFromDB(slideShowItem): FormArray {
const formArray = new FormArray([]);
slideShowItem.forEach(item => {
let group = this.fb.group({
id: [''],
slideShowId: [''],
pageType: ['1', Validators.required],
linkUrl: ['', Validators.required],
pageTitle: ['']
})
group.patchValue({
id: item.Id,
slideShowId: item.SlideShowId,
pageType: item.PageType,
linkUrl: item.LinkUrl,
pageTitle: item.PageTitle
});
formArray.push(group);
});
return formArray;
}