EventEmitter Service
Catching the event
Section titled “Catching the event”Create a service-
import {EventEmitter} from 'angular2/core';export class NavService { navchange: EventEmitter<number> = new EventEmitter(); constructor() {} emitNavChangeEvent(number) { this.navchange.emit(number); } getNavChangeEmitter() { return this.navchange; }}Create a component to use the service-
import {Component} from 'angular2/core';import {NavService} from '../services/NavService';
@Component({ selector: 'obs-comp', template: `obs component, item: {{item}}` }) export class ObservingComponent { item: number = 0; subscription: any; constructor(private navService:NavService) {} ngOnInit() { this.subscription = this.navService.getNavChangeEmitter() .subscribe(item => this.selectedNavItem(item)); } selectedNavItem(item: number) { this.item = item; } ngOnDestroy() { this.subscription.unsubscribe(); }}
@Component({ selector: 'my-nav', template:` <div class="nav-item" (click)="selectedNavItem(1)">nav 1 (click me)</div> <div class="nav-item" (click)="selectedNavItem(2)">nav 2 (click me)</div> `,})export class Navigation { item = 1; constructor(private navService:NavService) {} selectedNavItem(item: number) { console.log('selected nav item ' + item); this.navService.emitNavChangeEvent(item); }}Live example
Section titled “Live example”A live example for this can be found here.
Class Component
Section titled “Class Component”@Component({ selector: 'zippy', template: ` <div class="zippy"> <div (click)="toggle()">Toggle</div> <div [hidden]="!visible"> <ng-content></ng-content> </div> </div>`})export class Zippy { visible: boolean = true; @Output() open: EventEmitter<any> = new EventEmitter(); @Output() close: EventEmitter<any> = new EventEmitter(); toggle() { this.visible = !this.visible; if (this.visible) { this.open.emit(null); } else { this.close.emit(null); } }}Class Overview
Section titled “Class Overview”class EventEmitter extends Subject { constructor(isAsync?: boolean) emit(value?: T) subscribe(generatorOrNext?: any, error?: any, complete?: any) : any}Emmiting Events
Section titled “Emmiting Events”<zippy (open)="onOpen($event)" (close)="onClose($event)"></zippy>