DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Event Binding in Angular 2

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.

Rathrola Prem Kumar user avatar by
Rathrola Prem Kumar
·
Jul. 18, 18 · Tutorial
Like (4)
Save
Tweet
Share
13.49K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  • 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.

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!

Binding (linguistics) AngularJS Event

Published at DZone with permission of Rathrola Prem Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • Simulating and Troubleshooting StackOverflowError in Kotlin
  • How to Cut the Release Inspection Time From 4 Days to 4 Hours

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: