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.
Join the DZone community and get the full member experience.
Join For FreeIn 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.
I recommend going through my previous articles from the beginning for a better understanding.
- Architecture Angular 2 Part: 1
- Getting Started with Angular 2 Part: 2
- Understanding Application Files in Angular 2 Part: 3
- Simple Hello world Application in Angular 2 Part: 4
- Creating our first component in Angular 2 Part: 5
- Style Components in Angular 2 Part: 6
- Date Binding in Angular 2 Part: 7
- Class and Style Bindings in Angular 2 Part: 8
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.
Code
<button(click)="onClick()">Click me </button>
In our class, let’s define the method, as shown below.
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:
When clicked a second time, the count increases, as shown below.
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.
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.
Code
<button (click)="onClick(demoInput.value)">Click me</button>
Pass the value to the onClick
value and log the value, as shown below.
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.
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>
Thank you for reading my article.
Happy coding!
Published at DZone with permission of Rathrola Prem Kumar. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments