Please help. I'm having trouble creating a directive that will always set text inputs to uppercase. It seems to be working looking at the user interface but the model binding is showing the last typed character to still in lowercase character.
below is a portion of my html:
<div>
<md-input-container fxFlex>
<textarea #listCode mdInput [(ngModel)]="listInfo.code" placeholder="List Code"
uppercase-code maxlength="50" rows="3"
required></textarea>
<md-hint align="end">{{listCode.value.length}} / 50</md-hint>
</md-input-container>
{{listInfo.code}}
</div>
below is the directive:
import { Directive } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: '[ngModel][uppercase-code]',
host: {
'(ngModelChange)': 'ngOnChanges($event)'
}
})
export class UppercaseCodeDirective {
constructor(public model: NgControl) {}
ngOnChanges(event) {
var newVal = event.replace(/[^A-Za-z0-9_]*/g, '');
newVal = newVal.toUpperCase();
this.model.valueAccessor.writeValue(newVal);
}
}