I'm trying to have an input field format/mask values as they are typed, while having the actual model retain the raw (or differently formatted) value. I'm thinking phone numbers etc, but for simplicity am using uppercase for testing.
I've tried a bunch of stuff, hoping its as simple as a directive. But can't seem to get the display value to depart from the form value.
plunk: http://plnkr.co/edit/VH5zn4S8q28CBpFutBlx?p=preview
Here's the directive:
@Directive({
selector: '[uppercase]',
host: {
'(input)': 'onInputChange()',
}
})
export class UppercaseDirective {
constructor(private model: NgFormControl) { }
onInputChange() {
let newValue = this.model.value.toUpperCase();
this.model.viewToModelUpdate(newValue);
this.model.valueAccessor.writeValue(newValue);
}
}
and the form:
<form [ngFormModel]='myForm'>
<input [ngFormControl]='myForm.controls.field' uppercase>
<div>
{{ myForm.value.field }}
</div>
</form>