Angular2 - Class - EventEmitter

EventEmitter 就是用于发出一个event,一般用于output

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

可以和Rxjs的 subscribe一起用,具体看 angular2 + redux 使用。里面会用 rxjs 和 immutable

传值方法主要是emit。
emit(value: T) { super.next(value); }

未来修改方向是Rxjs,来替代emit等方法

subscribe(generatorOrNext?: any, error?: any, complete?: any): any {
let schedulerFn;
let errorFn = (err: any) => null;
let completeFn = () => null;

if (generatorOrNext && typeof generatorOrNext === 'object') {
schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext.next(value)); } :
(value) => { generatorOrNext.next(value); };

if (generatorOrNext.error) {
errorFn = this._isAsync ? (err) => { setTimeout(() => generatorOrNext.error(err)); } :
(err) => { generatorOrNext.error(err); };
}

if (generatorOrNext.complete) {
completeFn = this._isAsync ? () => { setTimeout(() => generatorOrNext.complete()); } :
() => { generatorOrNext.complete(); };
}
} else {
schedulerFn = this._isAsync ? (value) => { setTimeout(() => generatorOrNext(value)); } :
(value) => { generatorOrNext(value); };

if (error) {
errorFn =
this._isAsync ? (err) => { setTimeout(() => error(err)); } : (err) => { error(err); };
}

if (complete) {
completeFn =
this._isAsync ? () => { setTimeout(() => complete()); } : () => { complete(); };
}
}

return super.subscribe(schedulerFn, errorFn, completeFn);
}