Установить фокус в инпут
Почитав как делается установка фокуса в input https://coderanch.com/t/675897/langu...us-Angular-app
Решила сделать директиву import { Directive, OnInit, Input, ElementRef, Renderer } from '@angular/core'; @Directive({ selector: '[appSetFocus]' }) export class SetFocusDirective implements OnInit { @Input('appSetFocus') isFocused: boolean; constructor(private hostElement: ElementRef, private renderer: Renderer) {} ngOnInit() { console.log('***SetFocusDirective***'); if (this.isFocused) { this.renderer.invokeElementMethod(this.hostElement.nativeElement, 'focus'); } } } Вот так применяю директиву в шаблоне <input [appSetFocus]="addInput"> addInput - флаг, который делаю true, когда хочу получить фокус в инпуте Но фокус не появляется Меня смущает, что в директиве все происходит в функции OnInit() и в консоле вижу, что срабатывает один раз Подскажите в чем ошибка |
@Directive({ selector: '[appSetFocus]' }) export class SetFocusDirective implements OnInit { @Input('appSetFocus') set isFocused(value: boolean) { if (value) { this.renderer.invokeElementMethod(this.hostElement.nativeElement, 'focus'); } } constructor(private hostElement: ElementRef, private renderer: Renderer) {} ngOnInit() { console.log('***SetFocusDirective***'); } } |
Спасибо, destus, работает!:)
|
Часовой пояс GMT +3, время: 22:29. |