RxJS allows you to combine streams in various ways. This lesson shows you how to take a click stream and combine it with a store stream to use a value from the store inside a reducer.

The logic is when we click the recall button, it will reset all the people's time to the current time.

First, bind the click event to recall$:

<button (click)="recall$.next()">Recall</button>

...

recall$ = new Subject();

We get the latest time from the time stroe:

    constructor(store:Store) {
this.time = store.select('clock');
this.people = store.select('people'); Observable.merge(
this.click$,
this.seconds$,
this.person$,
this.recall$
.withLatestFrom(this.time, (_, y) => y) // _: means don't need to care about the first param which is this.recall$
.map( (time) => ({type: RECALL, payload: time}))
)
.subscribe(store.dispatch.bind(store))
}

_: is naming convention, it means, don't care about the first value.

Last, we handle the action in reducer:

        case RECALL:
return state.map( (person) => {
return {
name: person.name,
time: payload
};
})

-------------

[Angular 2] Using a Value from the Store in a Reducer的更多相关文章

  1. [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State

    ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...

  2. 手把手教你用ngrx管理Angular状态

    本文将与你一起探讨如何用不可变数据储存的方式进行Angular应用的状态管理 :ngrx/store——Angular的响应式Redux.本文将会完成一个小型简单的Angular应用,最终代码可以在这 ...

  3. [Angular 2] Using a Reducer to Change an Object's Property Inside an Array

    Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...

  4. [Angular 2] Passing Observables into Components with Async Pipe

    The components inside of your container components can easily accept Observables. You simply define ...

  5. Redux系列01:从一个简单例子了解action、store、reducer

    其实,redux的核心概念就是store.action.reducer,从调用关系来看如下所示 store.dispatch(action) --> reducer(state, action) ...

  6. 【React】Redux入门 & store体验

    组件间传值联动是令人头疼的问题,尤其是一个组件影响多个其他组件状态变化的时候,常常需要一级一级与父组件传值,与父组件的兄弟组件传值等等, 如何化繁为简地处理‘牵一发动全身’的清理就是将所有组件的sta ...

  7. Redux教程3:添加倒计时

    前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...

  8. 如何在AngularX 中 使用ngrx

    ngrx 是 Angular框架的状态容器,提供可预测化的状态管理. 1.首先创建一个可路由访问的模块 这里命名为:DemopetModule. 包括文件:demopet.html.demopet.s ...

  9. angular2 学习笔记 ( 状态管理 state management )

    更新 : 2017-12-29  ng5 移除 zone.js https://zhuanlan.zhihu.com/p/29577461 zone 的用途就是拦截游览器事件, 比如 click, a ...

随机推荐

  1. sql server identity限制

    identity属性是依赖于表的,它不是一种独立的序列机制,不能随意使用它生成新值. 标识值是在insert语句执行时生成的,不是在执行之前生成的. identity属性是以异步的方式分配标识值.这意 ...

  2. XML约束图解

  3. 九度OJ 1025 最大报销额(01背包)

    题目1025:最大报销额 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2683 解决:608 题目描述: 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具( ...

  4. underscorejs-each学习

    2.1 each 2.1.1 语法: _.each(list, iteratee, [context]) 2.1.2 说明: 依次对集合的所有元素进行某种操作,原样返回list.接收3个参数,list ...

  5. Cookie、LocalStorge、SesstionStorge 的区别和用法

    前言 总括:详细讲述Cookie.LocalStorge.SesstionStorge的区别和用法. 1. 各种存储方案的简单对比 Cookies:浏览器均支持,容量为4KB UserData:仅IE ...

  6. 用webclient.DownloadFile下载exe文件时大小为0

    用自己写的下载软件从服务器端下载文件,别的文件能下,但exe文件显示下载文件大小为0,连接超时,原因是服务上发布的下载文件夹的虚拟目录的属性有问题, 包含.exe 文件的虚拟目录已启用执行应用程序权限 ...

  7. Nginx源码研究六:NGINX的配置文件分析

    上一篇写到nginx的各个模块的配置信息的存储结构,大体描述了对配置信息的配置项生成,定制,初始化过程.这里重点研究实现定制的过程,所谓实现定制,这里指的是,nginx系统提供使用者定义nginx的配 ...

  8. Expires、Last-Modified、Etag缓存控制

    当请求一个页面时,如果浏览器使用本地缓存,因此我们经常会看到一个HTTP请求为304状态.或者显示200状态,在chrome下标注是from cache,在火狐下会标注BFCache: 我们希望在服务 ...

  9. IOS中扩展机制Category和associative

    在ios开发中,有时候会遇到以下的问题,需要在一个类中添加自己的一些属性和方法.一般的做法是重写一个类来继承它,但是有时候就只是需要添加一些简单的属性和方法,那么这样做就显得过于麻烦,其实在IOS中还 ...

  10. Ant快速入门(四)-----Ant的任务(Task)

    到目前为止,我们已经掌握了Ant生成文件的基本结构,以及<project.../>,<target.../>,<property.../>等元素的配置方式.而< ...