How to call a function on click event in Angular2 ?
Last Updated :
28 Apr, 2025
A Function is a set of statements that takes input, does some specific computation, and produces output. An on click event occurs when the user clicks on an element. In this article, we will see How to call a function on click event in Angular2, along with understanding the basic implementation through the examples.
Setting the Angular Application
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Project Structure
It will look like the following:

Example 1: In this example, we will call an API, and then iterate over the result, and display the data from the API in a card
HTML
<!-- app.component.html -->
<head>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<div style="width: 70%;">
<h2 style="color: green">
GeeksforGeeks
</h2>
<h2>
How to call a function on
click event in Angular2 ?
</h2>
<button class="btn btn-primary"
(click)="display()">
Call API
</button>
<div class="card">
<div class="card-header">
<b class="text-center">
Data Obtained from API below
</b>
</div>
<div class="card-body">
<div *ngFor="let item of gfg|keyvalue">
{{item.value}}
</div>
</div>
</div>
</div>
JavaScript
// app.component.ts
import { Component, OnInit }
from '@angular/core';
import { KeyValue }
from '@angular/common';
import { Pipe, PipeTransform }
from '@angular/core';
import { HttpClient }
from '@angular/common/http';
import { TmplAstHoverDeferredTrigger }
from '@angular/compiler';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent {
gfg: any
constructor(private http: HttpClient) { }
display() {
this.http.get('https://example.com').subscribe(
(result) => {
this.gfg = result
}
)
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Example 2: In this example, we will fire an alert on click.
HTML
<!-- app.component.html -->
<head>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<div style="width: 70%;">
<h2 style="color: green">
GeeksforGeeks
</h2>
<h2>
How to call a function on
click event in Angular2 ?
</h2>
<button class="btn btn-primary"
(click)="fireAlert()">
Fire Alert
</button>
</div>
JavaScript
// app.component.ts
import { Component, OnInit }
from '@angular/core';
import { KeyValue }
from '@angular/common';
import { Pipe, PipeTransform }
from '@angular/core';
import { HttpClient }
from '@angular/common/http';
import { TmplAstHoverDeferredTrigger }
from '@angular/compiler';
@Component({
selector: 'app-root',
templateUrl: "./app.component.html",
styleUrls: ['./app.component.css']
})
export class AppComponent {
gfg: any
constructor(private http: HttpClient) { }
fireAlert() {
alert("Hi Geek! Please vote for this article for GPL 2023")
}
}
JavaScript
// app.module.ts
import { NgModule }
from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { HttpClientModule }
from '@angular/common/http';
import { AppComponent }
from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Output:

Similar Reads
How to call an AngularJS Function inside HTML ? A Function is a set of statements that takes input, does some specific computation, and produces output. In this article, we will learn How to Call an AngularJS function inside HTML. To achieve this, we can use {{...}} to call the function from HTML. We can also pass arguments and return the result
3 min read
How to Scroll to an Element on click in Angular ? In this article, we will see how to scroll to an element on click in Angular. Here, we will create a component that enables scrolling to specific targets when a button is pressed within the document from one target to another.Steps for Installing & Configuring the Angular ApplicationStep 1: Crea
4 min read
How to add many functions in one ng-click directive? The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can popup alert when the button is clicked. The ng-click directive is a very handy tool used in AngularJS. When an HTML is clicked, the ng-click directive t
2 min read
How to get original element from ng-click in AngularJS ? In AngularJS, we can get the original DOM element that fired the ng-click event using the event's currentTarget property. Getting the event's currentTarget can be useful when we want to track an element's activity, in this case, whether and how many times a particular element is clicked, or we want
3 min read
How to directly update a field by using ng-click in AngularJS ? In this article, we will see how to update the field directly with the help of the ng-click directive in AngularJS, along with understanding different ways to implement it through the implementations. Any field can be updated with ng-click using a custom JavaScript function. For this, we can make a
3 min read