I have a directive in Angular that should read it's host element's input selectionStart, selectionEnd values. I do this in my custom directive:
@Directive({ selector: '[inputBehavior]', }) export class InputBehaviorDirective { public constructor( private el: ElementRef ) {} @HostListener('keydown', ['$event']) onKeyDown(event: KeyboardEvent): void { let { selectionStart, selectionEnd } = this.el.nativeElement as HTMLInputElement; } } host element's template looks like this:
<div class="input-field"> <div class="container"> <input [formControl]="formElement" /> </div> </div> host's typescript file:
@UntilDestroy() @Component({ selector: 'input-field', templateUrl: './input-field.component.html', styleUrls: ['./input-field.component.scss'], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => InputFieldComponent), multi: true, }, ], }) export class InputFieldComponent implements ControlValueAccessor { public formElement = new FormControl(); public writeValue(obj: number | string | undefined): void { this.formElement.setValue(obj?.toString()); } public registerOnChange(fn: any): void { } public registerOnTouched(fn: any): void {} public setDisabledState?(isDisabled: boolean): void {} } i use directive like this:
<input-field InputBehavior formControlName="height" > </input-field> but selectionStart and selectionEnd stay undefined. I want to apply the directive to the ControlValueAccessor host element as whole and not to the native html input element within host element's tempalte. So NOT like this:
<div class="input-field"> <div class="container"> <input InputBehavior [formControl]="formElement" /> </div> </div> so my question is, how to read selectionStart and selectionEnd values of the host element by applying the directive to it?