[Angular 2] Handling Click Events with Subjects
2016-04-26 03:26
645 查看
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.
So here create a click$ subject, every time you click the button, it will map to a current date value.
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] Rendering an Observable Date with the Async and Date Pipes
- [Angular 2] Rendering an Observable with the Async Pipe
- 学习AngularJs:Directive指令用法(完整版)
- AngularJS实现textarea记录只能输入规定数量的字符并显示
- Part 20 Create custom service in AngularJS
- Part 19 AngularJS Services
- AngularJS快速入门
- [置顶] angularJS学习小结——ngRoute(路由机制)
- angular directive
- angularjs自定义指令(下)
- [Angularjs]ng-switch用法
- angularjs file upload插件使用总结
- angular 自定义filter
- angular自定义指令(中)
- angularJs学习资料
- angularJS实现textarea记录只能输入规定数量的字符并显示
- Angularjs中UI Router全攻略
- angularjs 上拉加载,下拉刷新数据功能
- angular api
- 清除angular模板缓存