this.profile我在 javascript 中的模型 ( ) 有一个名为 的属性emails,它是一个数组{email, isDefault, status}
然后我将其定义如下
this.profileForm = this.formBuilder.group({
.... other properties here
emails: [this.profile.emails]
});
console.log(this.profile.emails); //is an array
console.log(this.profileForm.emails); // undefined
在 html 文件中,我将其用作
<div *ngFor="let emailInfo of profileForm.emails">
{{emailInfo.email}}
<button (click)="removeEmail(emailInfo)">
Remove
</button>
</div>
如果我不添加它formGroup并将其用作数组 - 如下所示 - 它工作正常,但是我有一个业务规则,该数组不应该为空,我无法根据长度设置表单验证这个
emails : [];
this.profileForm = this.formBuilder.group({
.... other properties here
});
this.emails = this.profile.emails;
console.log(this.profile.emails); //is an array
console.log(this.emails); // is an array
我也尝试过使用formBuilder.array,但那个是用于拥有控件数组而不是数据数组。
emails: this.formBuilder.array([this.profile.emails])
那么我的问题是我应该如何将一个数组从模型绑定到 UI 以及我应该如何验证数组的长度?