My model (this.profile
) in javascript has a property called emails
, which is an array of {email, isDefault, status}
Then I defined it as below
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
in html file I used it as
<div *ngFor="let emailInfo of profileForm.emails">
{{emailInfo.email}}
<button (click)="removeEmail(emailInfo)">
Remove
</button>
</div>
If I don't add it in formGroup
and use it as an array - like below - it's working fine, but then I have a business rule that this array shouldn't be empty and I am unable to set form validation based on length of this
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
also I tried using formBuilder.array
but that one is for having array of controls not array of data.
emails: this.formBuilder.array([this.profile.emails])
then my question is how should I bind an array from model to UI and how should I validate the length of array?
this.emails
? You can use minLength to validate theformArray
length with the latest versions of Angular. – Ramayana[{email:xx,isDefault:false},{email:yy,isDefault:true}]
, how can I define it in formArray and access it in html, because right now the value become undefined? – Darky