angularangular-formscontrolvalueaccessor

ControlValueAccessor and FormControl Value are out of sync


I have implemented a mask on my input to allow only a special format. The mask is working correctly, but when the user enters letters, for example, it is attached to the formControl value, even when the field does not show it.

Stackblitz


Solution

  • accessor.writeValue is a way to update the value inside of the input, but not in the logical model. The shortest fix is

    if(newVal != event) this.ngControl.control.setValue(newVal);
    

    this will update the value inside of the control