I am trying to use emitEvent as false on a Form Control of a FormArray to change ion-select option value to a specific one without running the change event again.
In my real app, the event will get values from database, but I created a simple stackblitz to describe the situation:
createForm(){
this.formGroup = this.fb.group({
formArray: this.createArray()
})
}
createArray()
{
return new FormArray(this.dummyData.map(item=> new FormGroup({
arrayControl: new FormControl(item.cid)
})))
}
change(array, index)
{
console.log('ionChange fired');
(<FormArray>this.formGroup.get('formArray')).at(index).get('arrayControl').patchValue(1, { onlySelf: true, emitEvent: false });
}
And I call createForm() in the constructor:
constructor(
public fb: FormBuilder,
public navCtrl: NavController) {
this.createForm();
}
Here is the html script:
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<div [formGroup]="formGroup" *ngIf="formGroup">
<ion-slides slidesPerView="2">
<ion-slide formArrayName="formArray" *ngFor="let array of dummyData; let i=index">
<ion-item [formGroupName]="i">
<ion-select formControlName="arrayControl" (ionChange)="change(array, i)">
<ion-option *ngFor="let list of data" [value]="list.id">{{list.name}}</ion-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>{{array.population}}</ion-label>
</ion-item>
<ion-item>
<ion-label>{{array.id}}</ion-label>
</ion-item>
</ion-slide>
</ion-slides>
</div>
</ion-content>
I need to sometimes to reset the whole form, so on reset, all ion-select values are set to their initial values, so the api call is ran again which I don’t need in this case as the initial data are saved in an array on client side.