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(),
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的更多相关文章

  1. [Angular 2] Handling Click Events with Subjects

    While Angular 2 usually uses event handlers to manage events and RxJS typically uses Observable.from ...

  2. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  3. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  4. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  5. [LeetCode] 435. Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  6. Android 自定义title 之Action Bar

    Android 自定义title 之Action Bar 2014-06-29  飞鹰飞龙...  摘自 博客园  阅 10519  转 25 转藏到我的图书馆   微信分享:   Action Ba ...

  7. 【Android】Android之Action Bar

    Action Bar是在窗口上指示用户位置的组件,同时给用户提供导航和操作.使用Action Bar可以让你的应用在不同配置的屏幕上看起来比较一致.在开始之前,先了解一些相关的术语: Action B ...

  8. ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理

    Adding Action Items The action bar provides users access to the most important action items relating ...

  9. 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 ...

随机推荐

  1. 微信返回上一页,页面中的AJAX的请求,对Get请求无效的解决办法

    问题产生原因       最近在做一个微信的项目时,遇到一种很常见的情况,需求是这样的,当用户进入到"我的个人中心"的时候,会有一个点击跳转填写认证资料的按钮,点击此按钮后,会跳转 ...

  2. hibernate_validator_03

    约束继承 如果要验证的对象继承于某个父类或者实现了某个接口,那么定义在父类或者接口中的约束会在验证这个对象的时候被自动加载,如同这些约束定义在这个对象所在的类中一样. 让我们来看看下面的示例: pac ...

  3. 用JAVA给JSON进行排版

    之前听到朋友的面试题,是如何对JSON进行排版,于是就写了一个Demo,觉得挺有意思的,就贴出来了. 这个就是记录缩进来输出,大家也可以尝试一下其他更好算法来进行输出. 功能:可以把一行的JSON字符 ...

  4. 你好,C++(31)我终于找到对象啦!6.1 从结构化设计到面向对象程序设计

    第6章 当C++爱上面向对象 很多第一次进入C++世界的人都会问:C++中的那两个加号到底是什么意思啊? C++是由C语言发展而来的,它比C语言多出的两个加号,实际上是C语言的自增操作符,表示C++语 ...

  5. 解决 Ubuntu15.04 登陆界面无限循环 的问题

    本人新手,在学习linux 安装NVIDIA 驱动的时候出现了一个奇怪的问题:登陆界面输入正确的账户密码,短暂闪烁后又返回了登陆界面.经查阅多种资料,已解决此问题 以下内容来自:http://blog ...

  6. php 文件操作之抓取网站图片

    $str= file_get_contents("http://v.qq.com/");preg_match_all("/\<img\s+src=.*\s*\> ...

  7. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  8. Ecshop后台菜单添加

    首先需要修改四个文件:inc_priv.php, inc_menu.php, priv_action.php, commn.php 假如当前的项要加在商品管理的菜单下 一:在languages/zh_ ...

  9. C语言笔记(枚举)

    关于枚举,在我以前写程序的时候,几乎是没有使用过的,只是偶尔会在一下别人写的驱动库中有看到过.今天看了朱老师的视频,准备将枚举相关的东西总结一下. 一.关于枚举,你需要知道 (1)在C语言中就是一些符 ...

  10. 关于System.out.println()与System.out.print("\n")的区别

    这是在写junit测试的时候发现的. import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class Te ...