码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] Handling Clicks and Intervals Together with Merge

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

标签:

Observable.merge allows you take two different source streams and use either one of them to make changes to the same state of your data. This lesson shows how you can use a stream of clicks and an interval stream and use either one to update the clock.

 

import {Component} from angular2/core;
import {bootstrap} from angular2/platform/browser;
import {Observable} from rxjs/Observable;
import rxjs/add/observable/interval;
import rxjs/add/observable/merge;
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();
    clock;
    constructor(){

        this.clock = Observable.merge(
            Observable.interval(5000),
            this.click$
        ).map( () => new Date());
    }
}

bootstrap(App);

 

So the logic is both 

  • every 5 seconds to update the clock
  • when click the button to update the clock

SO there use logic "OR" --> merge() to do that

[Angular 2] Handling Clicks and Intervals Together with Merge

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5433553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!