An event handler is specified inside the template using round brackets to denote event binding. This event handler is then coded in the class to process the event.
import {Component} from '@angular/core';
@Component({
selector: 'rio-counter',
template: `
<div>
<p>Count: {{num}}</p>
<button (click)="increment()">Increment</button>
</div>
`
})
export class CounterComponent {
num = 0;
increment() {
this.num++;
}
}
To send data out of components via outputs, start by defining the outputs attribute. It accepts a list of output parameters that a component exposes to its parent.
app/counter.component.ts
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'rio-counter',
templateUrl: 'app/counter.component.html'
})
export class CounterComponent {
@Input() count = 0;
@Output() result = new EventEmitter<number>();
increment() {
this.count++;
this.result.emit(this.count);
}
}
Together a set of input + output bindings define the public API of your component. In our templates we use the [squareBrackets] to pass inputs and the (parenthesis) to handle outputs.