我在javascript中的模型(this.profile
)有一个名为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
中添加它并将其用作数组 - 如下所示 - 它工作正常,但是我有一个业务规则,这个数组不应该&#39;是空的,我无法根据此
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以及如何验证数组的长度?
答案 0 :(得分:5)
我应该如何将数组从模型绑定到UI?
好吧,我更愿意将所有电子邮件从profile.emails
推送到formArray
,否则您将拥有这些值,但不会进行验证。
我应该如何验证数组的长度?
您可以将Validators.minLength(Number)
用作任何其他控件。
演示代码:
<强>组件:强>
export class AnyComponent implements OnInit {
profileForm: FormGroup;
emailsCtrl: FormArray;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.emailsCtrl = this.formBuilder.array([], Validators.minLength(ANY_NUMBER));
this.profile.emails.forEach((email: any) => this.emailsCtrl.push(this.initEmail(email)));
this.profileForm = this.formBuilder.group({
// ... other controls
emails: this.emailsCtrl
});
}
private initEmail = (obj: any): FormGroup => {
return this.formBuilder.group({
'email': [obj.email], //, any validation],
'isDefault': [obj.isDefault] //, any validation]
});
}
}
<强>模板:强>
<div *ngFor="let emailInfo of emailsCtrl.value">
{{emailInfo.email}}
<button (click)="removeEmail(emailInfo)">
Remove
</button>
</div>
<div *ngIf="emailsCtrl.hasError('minlength')">
It should have at least {{emailsCtrl.getError('minlength').requiredLength}} emails
</div>
PS1:请注意,Validators.minLength(param)
方法的参数必须大于1,否则无法验证。
正如您在source中看到控件为空时,它会自动返回null。
然后,为了使其按预期工作,您可以添加required
验证器:
this.emailsCtrl = this.formBuilder.array([], Validators.compose([Validators.required, Validators.minLength(ANY_NUMBER > 1)]);
在模板中:
<div *ngIf="emailsCtrl.invalid">
<span *ngIf="emailsCtrl.hasError('required')">
It's required
</span>
<span *ngIf="emailsCtrl.hasError('minlength')">
It should have at least {{emailsCtrl.getError('minlength').requiredLength}} emails
</span>
</div>
<强> PS2:强>
我认为在removeEmail
功能中传递要删除的电子邮件的索引更有意义,因此您无需致电indexOf
获取特定 email
的索引。你可以这样做:
<div *ngFor="let emailInfo of emailsCtrl.value; let i = index">
{{emailInfo.email}}
<button (click)="removeEmail(i)">
Remove
</button>
</div>
<强>组件:强>
removeEmail(i: number): void {
this.emailsCtrl.removeAt(i);
}
看看这个简单的 DEMO
答案 1 :(得分:2)
这对我有用(棱角分明2.1.2),这种方法可让您灵活地为电子邮件定义自定义验证:
this.profileForm = this.formBuilder.group({
emails: [this.profile.emails, FormValidatorUtils.nonEmpty]
// ......
});
export class FormValidatorUtils {
static nonEmpty(control: any) {
if (!control.value || control.value.length === 0) {
return { 'noElements': true };
}
return null;
}
}