Aniruddha Das
Aniruddha Das

Reputation: 21698

How to set autofocus in angular or angular 2 not angularjs

I see many posts in how to set autofocus on an input filed in angularjs by creating a directive or HTML autofocus, Is there a quick and easy way to set focus in angular (angular 2, angular 4 etc)

Upvotes: 9

Views: 28564

Answers (6)

Wadi Diaz-wong
Wadi Diaz-wong

Reputation: 121

This one did it for me, as my input element could show or hide depending on some property.

Template:

<input #captionElement matInput>

Code:

@ViewChild('captionElement') captionField: ElementRef;

If the item will show/hide (via *ngIf) like:

<ng-container *ngIf="editModeCaption; then editCaption else viewCaption" >
</ng-container>
<ng-template #editCaption>
  <mat-form-field>
   <mat-label>Image Caption</mat-label>
     <input  #captionElement matInput>
  </mat-form-field>
</ng-template>
<ng-template #viewCaption>
   ...
</ng-template>

Where editModeCaption is a property of your container (tells me whether I am in edit mode or not) component. DO NOT put it on ngAfterViewInit(), but on ngAfterViewChecked (AfterViewChecked interface). like this:

ngAfterViewChecked(): void {
  if (this.editModeCaption) {
    this.captionField.nativeElement.focus();
  }
}

Upvotes: 0

Sergey Gurin
Sergey Gurin

Reputation: 1573

<input id="name" type="text" #myInput />
{{ myInput.focus() }}

Add {{ myInput.focus() }} into your template.

Upvotes: 1

Pedro Gabriel
Pedro Gabriel

Reputation: 41

you can use the exemple of the most votted answer, but some times you need to put your focus inside of a setTimeout for make a "async" call. In my case i need to put the setTimeout in a selected filter of mat-autocomplete component of angular material.

my code look like this

   setTimeout(() => this.qtdeRef.nativeElement.focus());

Upvotes: 0

SuttonY
SuttonY

Reputation: 393

Aniruddha Das' answer works well in some cases. However, if this is applied to a template-based form with required validation, you may get the following error:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after >it was checked. Previous value: 'false'. Current value: 'true'.

One solution to this is wrapping the focus() call within ngInit() instead of ngAfterViewInit(). So, extending upon Aniduddha's answer:

In your html add the #namedReference to the component:

<input type="text"
    placeholder="Your full name"
    name="name"
    ngModel
    #fullName="ngModel"
    required
    #nameit>

Then use onNgInit() to apply auto focus into it:

@ViewChild('nameit') private elementRef: ElementRef;

ngOnInit() {
    this.elementRef.nativeElement.focus();
}

Upvotes: 9

Aniruddha Das
Aniruddha Das

Reputation: 21698

In your html add #nameit to get its reference in component code.

<input type="text" name="me" #nameit>

Then apply auto fouces into it.

  @ViewChild('nameit') private elementRef: ElementRef;

  public ngAfterViewInit(): void {
    this.elementRef.nativeElement.focus();
  }

Upvotes: 20

Bo Chen
Bo Chen

Reputation: 156

<input type="text" #textInput placeholder="something" (focus)="someFunction(textInput.value)"/>

or

<input type="text" #textInput placeholder="something" (keyup.enter)="someMethod(textInput.value)"/>

Upvotes: -2

Related Questions