[Angular 2] Handling Clicks and Intervals Together with Merge

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

时间: 2024-10-11 07:35:01

[Angular 2] Handling Clicks and Intervals Together with Merge的相关文章

[Angular 2] Handling Click Events with Subjects

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

Android设计和开发系列第二篇:Action Bar(Develop—API Guides)

Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an icon Adding Action Items Handling clicks on action items Using split action bar Navigating Up with the App Icon Adding an Action View Handling collap

Android 自定义title 之Action Bar

Android 自定义title 之Action Bar 2014-06-29  飞鹰飞龙...  摘自 博客园  阅 10519  转 25 转藏到我的图书馆 微信分享: Action Bar是在窗口上指示用户位置的组件,同时给用户提供导航和操作.使用Action Bar可以让你的应用在不同配置的屏幕上看起来比较一致.在开始之前,先了解一些相关的术语: Action Bar有以下几项关键功能: 1)为你的App提供一个装饰处,同时也可以让用户知道自己的所在位置: 2)让一些重要的操作以一种可预

Airbnb 面试题汇总

Palindrome Pairs warm up:is_palindrome bool isPalindrome(string s) { int left = 0, right = s.size() - 1; while (left < right) { if (s[left++] != s[right--]) return false; } return true; } 给定一个字符串数组,找出所有的字符串对,该字符串对拼接起来是回文字符串(https://leetcode.com/probl

[Angular] Angular Global Keyboard Handling With EventManager

If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. Now we have a modal component, we want to click 'Esc' key to close the modal. <au-modal class="auth-modal" *auModalOpenOnClick="[loginBut

Angular DirtyChecking(脏值检查) $watch, $apply, $digest

Dirty Checking (脏值检查) Digest cycle and $scope Digest cycle and $scope First and foremost, AngularJS defines a concept of a so-called digest cycle. This cycle can be considered as a loop, during which AngularJS checks if there are any changes to all t

带你进入Angular js的大门

首先需要指出什么是angular js,其实说白了angular js就是Javascript的一个类库,我们使用这个类库可以很容易的创建web页面.双向绑定是angular js其中的一个重要特征,这也是相对于其他的Javascript的类库来说angular js中很重要的特征.双向绑定即是当你修改任何属性的值的时候,相关联的html元素也将改变,你并不需要额外的去修改. Angular js还为我们提供了MVVM(Model View ViewModel)的模型.MVVM的意思就是说Mod

Angular(2+) 国际化方案(ngx-translate)

本文只针对ngx-translate/core 6.x版本,如果你使用的是5.x或者更低的版本,请参照以下链接. https://github.com/ngx-translate/core/blob/fb02ca5920aae405048ebab50e09db67d5bf12a2/README.md 安装 首先需要安装npm依赖: npm install @ngx-translate/core --savenpm install @ngx-translate/http-loader --save

All About Angular 2.0

angular All About Angular 2.0 Posted by Rob Eisenberg on  November 6th, 2014. Have questions about the strategy for Angular 2.0? This is the place. In the following article I'll explain the major feature areas of Angular 2.0 along with the motivation