I often need to add a setTimeout to make an instruction run the right way with Ionic.
My config :
Ionic:
ionic (Ionic CLI) : 4.0.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.1.1
@ionic/app-scripts : 1.3.7
System:
NodeJS : v6.10.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/bin/node)
npm : 3.10.10
OS : macOS High Sierra
My template :
<ion-list [formGroup]="formGroup">
<ion-item *ngIf="countries">
<ion-label floating>
{{ 'customer.country' | translate }}
<span class="required" ion-text color="red">*</span>
</ion-label>
<ion-select formControlName="Country">
<ion-option *ngFor="let c of countries" [value]="c.In">{{c.Out}}</ion-option>
</ion-select>
</ion-item>
For example if I try to set a form control value like this, it will not work properly without a setTimeout:
private setFormControlValue(propertyName: string, value: string, stockEmpty?: boolean): void {
let formControl: AbstractControl = this.formGroup.controls[propertyName];
if (_.isEmpty(value) && !stockEmpty) {
return;
} else {
if (!_.isEmpty(formControl) && !_.isNil(formControl)) {
this.logger.trace('Setting value %s for control : ', value, formControl);
setTimeout(() => formControl.setValue(value), 100);
}
}
}
Also happen when I want to resize my content :
ngOnInit(): void {
setTimeout(() => this.content.resize(), 100);
}
It even works sometimes with a timeout of '0' ms. I would like to avoid using timeout everywhere but I have the impression Ionic needs the instruction to be run in a different 'thread' to be executed the right way.
If someone does encounter the same issue or know a better workaround I would gladly take it.