Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Angular 10 (focus) Event

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we are going to see what is focus event in Angular 10 and how to use it. The (focus) event is triggered when an element gains its focus.

Syntax:

<input (focus)='functionName()'/>

NgModule: Module used by focus event is:

  • CommonModule

Approach: 

  • Create an Angular app to be used.
  • In app.component.ts make a function that triggers on focus event.
  • In app.component.html make an input element and set focus event.
  • Serve the angular app using ng serve to see the output.

 

Example 1:

app.component.ts




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
  
export class AppComponent {
    onFocus(): void {
        console.log('Focus Is gained for this Element');
    }
}

app.component.html




<br>
<form>
    <input placeholder="Name" (focus) = 'onFocus()'>
</form>

Output:

Example 2:

app.component.ts




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
  
export class AppComponent {
    onFocus(): void {
        console.log('Focus Is gained for this Element');
    }
}

app.component.html




<br>
<form>
    <button (focus) = 'onFocus()'>Click here!</button>
</form>

Output:


My Personal Notes arrow_drop_up
Last Updated : 04 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials