Setting focus on an input field after ng-show

后端 未结 5 785
遥遥无期
遥遥无期 2020-12-13 13:41

Is it possible set the focus of an input field via a controller once data has loaded (via $resource in this case)?

The input is hidden via ng-show until the data is

5条回答
  •  無奈伤痛
    2020-12-13 14:08

    Another version of this for Angular.io

    import {Directive, ElementRef, Input, OnChanges, SimpleChanges} from '@angular/core';
    
    //Directive to that toggles focus from boolean value set with focusOn
    @Directive({
      selector: '[focusOn]'
    })
    export class FocusOnDirective implements OnChanges {
      @Input('focusOn') setFocus: boolean;
    
      constructor(private hostElement: ElementRef) {}
    
      ngOnChanges(changes : SimpleChanges) {
        (!!changes.setFocus && !!changes.setFocus.currentValue) ?
          this.hostElement.nativeElement.focus() :
          this.hostElement.nativeElement.blur();
      }
    }
    

提交回复
热议问题