I have successfully set up a Formarray that has two controls. The controls are set in the HTML form as select objects. I can successfully push and display multiple element sets but when I try to change the value of the select object in the first element of the Formarray the other Formarray element select objects take on the same value. I am using [(ngModel)] to bind a value to the object and I believe that is why the vales are always the same. I tried using an array variable with [(ngModel)]=stringArray[i] using the Formarray index, but I get errors when the page is loaded.
Can anyone suggest how to get the value of the select object using [(ngModel)] or other mechanism? I am using an ng-lightning select component (version 1.3.0) and SLDS Lightning Design CSS. I am using Angular version 4.1.3. In other parts of my app, I need to use [(ngModel)] with a string in order to get the value of a select object. The select values are an array of objects and not a primitive variable like a string. The component does not have an onSelect() function defined.
HTML:
<form [formGroup]="callFlowForm">
<div formArrayName="callDevices">
<!-- Check the correct way to iterate your form array -->
<div *ngFor="let callDevice of callFlowForm.controls.callDevices.controls; let i=index" [formGroupName]="i">
<div class="form-group">
<div class="slds-form-element slds-size--8-of-8">
<ngl-form-element label="Device #: {{ i + 1 }}" class="slds-m-top--small">
<select nglFormControl class="slds-select"
formControlName="callAsset"
[(ngModel)]="selectedDevice"
ngDefaultControl >
<option *ngFor="let device of devices"
[value]="device.id">{{device.assetName}}
</option>
</select>
</ngl-form-element>
</div>
</div>
<div class="form-group">
<div class="slds-form-element slds-size--8-of-8">
<ngl-form-element label="Protocol:" class="slds-m-top--small">
<select nglFormControl class="slds-select"
formControlName="assetTransport"
[(ngModel)]="selectedTransport"
ngDefaultControl >
<option *ngFor="let protocol of transports"
[value]="protocol.id">{{protocol.type}}
</option>
</select>
</ngl-form-element>
</div>
</div>
<button *ngIf="callFlowForm.controls.callDevices.controls.length > 1" (click)="deleteDevice(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button type="button" (click)="addDevice()" class="btn btn-primary">Add Device</button>
</form>
Component:
selectedTransport: string;
selectedDevice: string;
public callFlowForm: FormGroup;
transports: SipTransport[] = [{'id': 1, 'type': 'UDP'},
{'id': 2, 'type': 'TCP'},
{'id': 3, 'type': 'TLS'}
];
devices: Asset[] = [{'id': 1, 'assetName': 'VCS', 'type': 'SIP Registrar'},
{'id': 1, 'assetName': 'CUCM', 'type': 'Call Control'},
{'id': 1, 'assetName': 'SBC', 'type': 'Call Control'},
{'id': 1, 'assetName': 'EX60', 'type': 'Endpoint'}
];
constructor(private dialService: DialService,
private formBuilder: FormBuilder
) { }
ngOnInit() {
this.selectedDevice = '1';
this.selectedTransport = '1';
this.callFlowForm = this.formBuilder.group({
callDevices: this.formBuilder.array([this.addDevices()]) // here
});
}
addDevices() {
return this.formBuilder.group({
callAsset: [''],
assetTransport: ['']
});
}
addDevice() {
const control = <FormArray>this.callFlowForm.controls['callDevices'];
control.push(this.addDevices());
}
deleteDevice(index: number) {
const control = <FormArray>this.callFlowForm.controls['callDevices'];
control.removeAt(index);
}