Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Angular2 event binding

Suggested Videos
Part 11 - Angular attribute binding | Text | Slides
Part 12 - Class binding in angular 2 | Text | Slides
Part 13 - Style binding in angular 2 | Text | Slides

In this video we will discuss Event Binding in Angular with examples.

The following bindings that we have discussed so far in this video series flow data in one direction i.e from a component class property to an HTML element property.
1. Interpolation
2. Property Binding
3. Attribute Binding
4. Class Binding
5. Style Binding

How about flowing data in the opposite direction i.e from an HTML element to a component. When a user performs any action like clicking on a button, hovering over an element, selecting from a dropdownlist, typing in a textbox etc, then the corresponding event for that action is raised. We need to know when user performs these actions. We can use angular event binding to get notified when these events occur.

For example the following is the syntax for binding to the click event of a button. Within parentheses on the left of the equal sign we have the target event, (click) in this case and on the right we have the template statement. In this case the onClick() method of the component class is called when the click event occurs.
<button (click)="onClick()">Click me</button>

With event binding we can also use the on- prefix alternative as shown below. This is known as the canonical form
<button on-click="onClick()">Click me</button>

Event Binding Example : 

import { Component } from '@angular/core';

    selector: 'my-app',
    template: `<button (click)='onClick()' >Click me</button>`
export class AppComponent {
    onClick(): void {
        console.log('Button Clicked');

Every time we click the button, 'Button Clicked' message is logged to the console. You can see this message under the Console tab, in the browser developer tools.
angular bind event to element

Another Example : Initially when the page loads we want to display only the First Name and Last of Employee. We also want to display "Show Details" button.

angular 2 click event example

When we click "Show Details" button, we want to display "Gender" and "Age" as well. The text on the button should be changed to "Hide Details". When we click "Hide Details" button, "Gender" and "Age" should be hidden and the button text should be changed to "Show Details".

angular 2 click event binding

To achieve this we will make use of event binding in Angular. We will also make use of one of the structural directives "ngIf" in angular.

Code in employee.component.ts : Notice we have introduced "showDetails" boolean property. The default value is false, so when the page first loads, we will have "Gender" and "Age" hidden. We also have a method, toggleDetails(), which toggles the value of showDetails. 

import { Component } from '@angular/core';

    selector: 'my-employee',
    templateUrl: 'app/employee/employee.component.html',
    styleUrls: ['app/employee/employee.component.css']
export class EmployeeComponent {
    columnSpan: number = 2;
    firstName: string = 'Tom';
    lastName: string = 'Hopkins';
    gender: string = 'Male';
    age: number = 20;
    showDetails: boolean = false;

    toggleDetails(): void {
        this.showDetails = !this.showDetails;

Code in employee.component.html :
  • Notice the click event of the button is binded to toggleDetails() method
  • To dynamicall change the text on the button, we are using ternary operator - {{showDetails ? 'Hide' : 'Show'}} Details
  • We used ngIf structural directive on "Gender" and "Age" <tr> elements 
  • The * prefix before a directive indicates, it is a structural directive
  • Besides ngIf, there are other structural directives which we will discuss in our upcoming videos
  • The ngIf directive conditionally adds or removes content from the DOM based on whether or not an expression is true or false
  • If "showDetails" is true, "Gender" and "Age" <tr> elements are added to the DOM, else removed
            <th attr.colspan="{{columnSpan}}">
                Employee Details
            <td>First Name</td>
            <td>Last Name</td>
        <tr *ngIf='showDetails'>
        <tr *ngIf='showDetails'>
<br />
<button (click)='toggleDetails()'>
    {{showDetails ? 'Hide' : 'Show'}} Details

Angular 2 tutorial for beginners

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.