Angular 2 - Handling Events


In Angular 2, events such as button click or any other sort of events can also be handled very easily. The events get triggered from the html page and are sent across to Angular JS class for further processing.

Let’s look at an example of how we can achieve event handling. In our example, we will look at displaying a click button and a status property. Initially, the status property will be true. When the button is clicked, the status property will then become false.

Step 1 − Change the code of the app.component.ts file to the following.

import { 
} from '@angular/core';  

@Component ({ 
   selector: 'my-app', 
   templateUrl: 'app/app.component.html' 

export class AppComponent { 
   Status: boolean = true; 
   clicked(event) { 
      this.Status = false; 

Following points need to be noted about the above code.

  • We are defining a variable called status of the type Boolean which is initially true.

  • Next, we are defining the clicked function which will be called whenever our button is clicked on our html page. In the function, we change the value of the Status property from true to false.

Step 2 − Make the following changes to the app/app.component.html file, which is the template file.

   <button (click) = "clicked()">Click</button> 

Following points need to be noted about the above code.

  • We are first just displaying the value of the Status property of our class.

  • Then are defining the button html tag with the value of Click. We then ensure that the click event of the button gets triggered to the clicked event in our class.

Step 3 − Save all the code changes and refresh the browser, you will get the following output.

Click True

Step 4 − Click the Click button, you will get the following output.

Click False

Useful Video Courses


AngularJS Online Training

16 Lectures 1.5 hours

Anadi Sharma


Angular 2 Online Training

28 Lectures 2.5 hours

Anadi Sharma


Step By Step Angular For Beginnners

11 Lectures 7.5 hours



Ionic & Angular JS: Principles Of Mobile and Web Development

16 Lectures 2.5 hours

Frahaan Hussain


Angular 8 Full Stack Development With Spring Boot

Best Seller

69 Lectures 5 hours

Senol Atac


JWT Role Based Authorization With Spring Boot And Angular 8

53 Lectures 3.5 hours

Senol Atac