Angular2 - Focusing a textbox on component load

前端 未结 10 1536
刺人心
刺人心 2020-12-02 16:29

I am developing a component in Angular2 (Beta 8). The component has a textbox and a dropdown. I would like to set the focus in textbox as soon as component is loaded or on c

相关标签:
10条回答
  • 2020-12-02 17:07

    Directive for autoFocus first field

    import {
      Directive,
      ElementRef,
      AfterViewInit
    } from "@angular/core";
    
    @Directive({
      selector: "[appFocusFirstEmptyInput]"
    })
    export class FocusFirstEmptyInputDirective implements AfterViewInit {
      constructor(private el: ElementRef) {}
      ngAfterViewInit(): void {
        const invalidControl = this.el.nativeElement.querySelector(".ng-untouched");
        if (invalidControl) {
          invalidControl.focus();
        }
      }
    }

    0 讨论(0)
  • 2020-12-02 17:09

    you can use $ (jquery) :

    <div>
        <form role="form" class="form-horizontal ">        
            <div [ngClass]="{showElement:IsEditMode, hidden:!IsEditMode}">
                <div class="form-group">
                    <label class="control-label col-md-1 col-sm-1" for="name">Name</label>
                    <div class="col-md-7 col-sm-7">
                        <input id="txtname`enter code here`" type="text" [(ngModel)]="person.Name" class="form-control" />
    
                    </div>
                    <div class="col-md-2 col-sm-2">
                        <input type="button" value="Add" (click)="AddPerson()" class="btn btn-primary" />
                    </div>
                </div>
            </div>
            <div [ngClass]="{showElement:!IsEditMode, hidden:IsEditMode}">
                <div class="form-group">
                    <label class="control-label col-md-1 col-sm-1" for="name">Person</label>
                    <div class="col-md-7 col-sm-7">
                        <select [(ngModel)]="SelectedPerson.Id"  (change)="PersonSelected($event.target.value)" class="form-control">
                            <option *ngFor="#item of PeopleList" value="{{item.Id}}">{{item.Name}}</option>
                        </select>
                    </div>
                </div>
            </div>        
        </form>
    </div>
    

    then in ts :

        declare var $: any;
    
        @Component({
          selector: 'app-my-comp',
          templateUrl: './my-comp.component.html',
          styleUrls: ['./my-comp.component.css']
        })
        export class MyComponent  {
    
        @ViewChild('loadedComponent', { read: ElementRef, static: true }) loadedComponent: ElementRef<HTMLElement>;
    
        setFocus() {
        const elem = this.loadedComponent.nativeElement.querySelector('#txtname');
              $(elem).focus();
        }
        }
    
    0 讨论(0)
  • 2020-12-02 17:10

    Also, it can be done dynamically like so...

    <input [id]="input.id" [type]="input.type" [autofocus]="input.autofocus" />
    

    Where input is

    const input = {
      id: "my-input",
      type: "text",
      autofocus: true
    };
    
    0 讨论(0)
  • 2020-12-02 17:15

    See Angular 2: Focus on newly added input element for how to set the focus.

    For "on load" use the ngAfterViewInit() lifecycle callback.

    0 讨论(0)
提交回复
热议问题