I have an array of objects and I am attempting to loop through the array, dynamically setting values to a formControl and not displaying anything if the value is null. I have searched for similar solutions but haven't found any references or examples so far.
.html
<form [formGroup]="testForm">
<div class="class" *ngFor="let item of items">
<span>{{ item.name }}</span>
<span>{{ item.email }}</span>
<div>
<input type="text" formControlName="number">
</div>
</div>
</form>
.ts
testForm: FormGroup;
items: any[] = [
{
name: 'Rob',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ad8e5e8cafeeff9fea4e9e5e7">[email protected]</a>',
number: 1234
},
{
name: 'Mack',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f426e6c644f7b6a7c7b216c6062">[email protected]</a>',
number: 9876
},
{
name: 'Mack',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5914383a32192d3c2a2d773a3634">[email protected]</a>',
number: null
}
];
get number() {
return this.testForm.get('number');
}
ngOnInit() {
this.testForm = this.formBuilder.group({
number: [this.items[0].number, [Validators.required]]
});
}