Hi,
I am facing a strange problem and I am kinda new to Ionic.
I am using Ionic 4 with Angular 7 with the form template, and the information of the system as follows:
System:
NodeJS : v10.16.0
npm : 6.9.0
OS : Windows 10
The problem is that after loading the data from the server and assign to a local object, the form control for the number variable becomes dirty but the control for the string variable is not. The correct behaviour is not to mark any control as dirty unless it changed from the form.
I figured out what is the problem. ion-input control treats the value internally as a string regardless of the input variable type, so when the input control compares the new input value (number in this case) with the stored value (string), the comparison fails.
This is where the stored value in the control
export class Input implements ComponentInterface {
…
/**
The value of the input.
*/ @Prop({ mutable: true }) value?: string | null = ‘’;
…
}
The comparison takes place inside the value accessor
export class ValueAccessor implements ControlValueAccessor {
…
handleChangeEvent(value: any) {
// ------- my comment ------------
// this line will give false in case of comparing string to number, “value” will be string and “this.lastValue” will be number
// ------- my comment ------------
if (value !== this.lastValue) {
this.lastValue = value;
this.onChange(value);
}
setIonicClasses(this.el);
}
…
}
To overcome this problem I have to change the variable type after loading to a string.
// Todo: workaround currently needed because numeric <ion-input> sets form model dirty
public convertToNumber(event: any): number {
return +(event.detail.value);
}
public convertToString(value: number): string {
return value.toString();
}