- , DefaultValueAccessor
, , . , , , DefaultValueAccessor
, , ( , , . ):
export const DEFAULT_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DefaultValueAccessor),
multi: true
};
@Directive({
selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]",
host: {"(input)": "onChange($event.target.value)", "(blur)": "onTouched()"},
providers: [DEFAULT_VALUE_ACCESSOR]
})
export class DefaultValueAccessor implements ControlValueAccessor {
onChange = (_: any) => {
};
onTouched = () => {
};
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {
}
writeValue(value: any): void {
const normalizedValue = value == null ? "" : value;
if ((this._elementRef.nativeElement as HTMLInputElement).value !== normalizedValue)
this._renderer.setElementProperty(this._elementRef.nativeElement, "value", normalizedValue);
}
registerOnChange(fn: (_: any) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, "disabled", isDisabled);
}
}
:
let start=this.el.nativeElement.selectionStart;
let end = this.el.nativeElement.selectionEnd;
this.model.valueAccessor.writeValue(newVal);
this.el.nativeElement.setSelectionRange(start,end);
, , ...