Книга: Angular 2 Cookbook
Назад: Binding to native element attributes
Дальше: Generating and capturing custom events using EventEmitter

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 .

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;   }   

How to do it...

The Angular 2 event binding syntax is accomplished with a pair of parentheses surrounding the event type. In this case, events that you wish to listen for will have a type property of click, and this is what they will be bound against. The value of the bound event attribute is an expression, so you can invoke the method as a handler within it:

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

How it works...

Angular watches for the event binding syntax (click) and adds a click listener to ArticleComponent, bound to the share() handler. When this event is observed, it evaluates the expression attached to the event, which in this case will invoke a method defined on the component.

There's more...

Since capturing the event must occur in an expression, you are provided with an $event parameter in the expression, which will usually be passed as an argument to the handler method. This is similar to the process in Angular 1. Inspecting this $event object reveals it as the vanilla click event generated by the browser:

[app/article.component.ts]      import {Component} from '@angular/core';      @Component({     selector: 'article',     template: `       <h1>{{title}}</h1>       <p>Shares: {{shareCt}}</p>       <button (click)="share($event)">Share</button>     `   })   export class ArticleComponent {     title:string = 'Police Apprehend Tiramisu Thieves';     shareCt:number = 0;     share(e:Event):void {       console.log(e);  // MouseEvent       ++this.shareCt;     }   }   

Note

You will also note that the share() method here is demonstrating how typing can be applied to the parameters and the return value of the method:

myMethod(arg1:arg1type, arg2:arg2type, ...):returnType

As with member binding, you are also able to use an alternate event binding syntax if you do not care to use a set of parentheses. Prefixing on- to the event attribute will provide you with identical behavior:

import {Component} from '@angular/core';      @Component({     selector: 'article',     template: `       <h1>{{title}}</h1>       <p>Shares: {{shareCt}}</p>       <button on-click="share($event)">Share</button>     `   })   export class Article {     title:string = 'Police Apprehend Tiramisu Thieves';     shareCt:number = 0;     share(e:Event):void {       ++this.shareCt;     }   }   

See also

  • Binding to native element attributes shows how Angular 2 interfaces with HTML element attributes.
  • Generating and capturing custom events using EventEmitter details how to propagate information upwards between components.
  • Attaching behavior to DOM elements with directives demonstrates how to attach behavior to elements with attribute directives.
  • Attribute property binding shows Angular 2's clever way of deep referencing element properties.
Назад: Binding to native element attributes
Дальше: Generating and capturing custom events using EventEmitter

thank you
Flame
cant read the code since it is all on a single line. Also this comments section is russian
Rakuneque
DATA COLLECTION AND ANALYSIS Two reviewers extracted data and assessed methodological quality independently lasix torsemide conversion Many others were in that space already