Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Event Binding in Angular 2

DZone's Guide to

Event Binding in Angular 2

A web developer discusses the importance of event binding in Angular, and gives a quick demonstration on how to use event binding.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

In our previous article, we learned about class and style binding, where the data flows from the component to the view. When the component class property changes, the view also changes.

Image title

I recommend going through my previous articles from the beginning for a better understanding.

In this article, we are going to learn about event binding and references.

Now, we are only setting the values to DOM element properties, but there is no way to retrieve DOM element properties.

For Example

There might be a situation where the user will fill out a form or click a button, which results in the flow of data from the view to our component's class. This is where event binding comes into the picture by helping us to capture the data flow from the view to the component.

In order to understand event binding better, let’s first create a button, as shown below.

Code

@Component({  
    selector: "my-tuts",  
    template: `<h2>{{title}}</h2>  
<button>Click me</button>`  
})

Just like how we have square brackets for property binding, for event binding we use parentheses, (), as shown below.

Image title

Code

<button(click)="onClick()">Click me </button>

In our class, let’s define the method, as shown below.

Image title

Code

onClick(){  
console.log('ButtonClicked'); 
}

Save this and run it -- the output should look like what I've got below.

When I click the button for the first time, the log in the console looks like this:

Image title

When clicked a second time, the count increases, as shown below.

Image title

Now, in Angular, it is very easy to reference HTML tags or the elements. Now, to reference an element, all we need to do is use the hash (#) symbol with any random variable, as shown below.

Image title

Code:

<input type=text #demoInput>`

In order to get the data which is flowing from an input tag, we can use a reference variable.

For Example

Let’s say, we passed demoInput.value as one of the parameters for the onClick method.

Image title

Code

<button (click)="onClick(demoInput.value)">Click me</button>  

Pass the value to the onClick value and log the value, as shown below.

Image title

Code

export class RathrolaComponent {  
    public title = "Tutorials from Rathrola";  
    onClick(value) {  
        console.log(value);  
    }  
}  

Save and run.

Key in some value, say, Hello world, and watch the console. It is going to log the input field, as shown below.

Image title

That is how event binding works; we have bound the button click event with a handler calledonClick. We are going to use the reference to input the elements. By passing its value, we can retrieve the value in our class.

Finally, to capture the event, we use $event, as shown below.

Code

<button (click)="onClick($event)">Click me</button>

Image title

Thank you for reading my article.

Happy coding!

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

Topics:
angular 2 ,web dev ,angular ,tutorial ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}