This is my FormGroup:
this.productGroup = this.fb.group({
name: ['', Validators.compose([Validators.required, Validators.maxLength(80)])],
variants: this.fb.array([
this.fb.group({
type: '',
options: this.fb.array([])
})
])
});
How to pass a string array to options? Like this [ 'string1', 'string2' ]. I'm dinamically getting those values here: stackblitz, however i'm not sure how to fill the array. I would like to pass a generic string array directly to options.
variants array example:
variants: [
{ type: 'Color', options: ['Red', 'Blue'] },
{ type: 'Size', options: ['Small', 'Medium', 'Big'] }
]
Html:
<form [formGroup]="productGroup">
<input formControlName="name">
<div formArrayName="variants" *ngFor="let item of productGroup.controls['variants'].controls; let i = index;">
<div [formGroupName]="i">
<mat-form-field>
<input type="text" placeholder="Variable Type" aria-label="Number" matInput formControlName="type" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let type of types" [value]="type">
{{type}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
<mat-form-field>
<mat-chip-list #chipList>
<mat-chip *ngFor="let opt of typesOptionsArray[i]" [selectable]="true"
[removable]="true" (removed)="removeOpt(opt, i)">
{{opt}}
<mat-icon matChipRemove>cancel</mat-icon>
</mat-chip>
<input placeholder="Type Options"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="true"
(matChipInputTokenEnd)="addOpt($event, i)">
</mat-chip-list>
</mat-form-field>
</div>
<div class="row">
<a href="javascript:" (click)="addItem()"> Add Variant </a>
<a href="javascript:" (click)="removeItem(i)" *ngIf="i > 0"> Remove Variant </a>
</div>
</div>
</form>
Angular Material MatChips. I get a new value everytime a user add a new chip (event), then add it to a string array in my component.this.typesOptionsArray[index].forEach(type => { this.productGroup.controls['variants']['options'].push(new FormControl(type)); });to pass the string array to FormArray directly