首页 > 其他 > 详细

[Angular 2] Handling Click Events with Subjects

时间:2016-04-26 07:05:06      阅读:112      评论:0      收藏:0      [点我收藏+]

While Angular 2 usually uses event handlers to manage events and RxJS typically uses Observable.fromEvent, a good practice when using Angular 2 and RxJS combined is to use Subjects and push the value through each event so that you can use it as part of your stream.

 

import {Component} from angular2/core;
import {bootstrap} from angular2/platform/browser;
import rxjs/add/operator/map;
import {Subject} from rxjs/Subject;

@Component({
    selector: app,
    template: `
        <button (click)="click$.next()">Update</button>
        <h1>{{clock | async | date: yMMMMEEEEdjms}}</h1>
    `
})

class App {

    click$ = new Subject();
    public clock;
    constructor(){
        this.clock = this.click$.map( () => new Date());
    }
}

bootstrap(App);

 

So here create a click$ subject, every time you click the button, it will map to a current date value.

 

[Angular 2] Handling Click Events with Subjects

原文:http://www.cnblogs.com/Answer1215/p/5433552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!