Registering handlers on native browser events

In Angular 2, the other hemisphere of binding that is needed for a fully functioning application is event binding. The Angular 2 event binding syntax is similar to that of data binding.

Note

The code, links, and a live example of this are available at http://ngcookbook.herokuapp.com/4437/.

Getting ready

Suppose you wanted to create an article application that counted shares, and you began with the following skeleton:

[app/article.component.ts] import {Component} from '@angular/core'; @Component({ selector: 'article', template: ` <h1>{{title}}</h1> <p>Shares: {{shareCt}}</p> <button>Share</button> ` }) export class ArticleComponent { title:string = 'Police Apprehend Tiramisu Thieves'; shareCt:number = 0; ...

Get Angular 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.