[Angular 2] Managing State in RxJS with StartWith and Scan
The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves just as a reduce function would, but scan is able to collect values from streams over time. This lesson covers using startWith to set the initial accumulator value then using scan to update the value of the clock from the clicks and interval.
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 'rxjs/add/operator/scan';
import 'rxjs/add/operator/startWith';
import {Subject} from 'rxjs/Subject';
@Component({
selector: 'app',
template: `
<button (click)="click$.next()">Add one second</button>
<h1>{{clock | async | date: 'yMMMMEEEEdjms'}}</h1>
`
})
class App {
click$ = new Subject();
clock;
constructor(){
this.clock = Observable.merge(
Observable.interval(),
this.click$
)
.startWith(new Date())
.scan( (acc: Date, curr) => {
const date = new Date(acc.getTime());
date.setSeconds(date.getSeconds() + );
return date;
});
}
}
bootstrap(App);
[Angular 2] Managing State in RxJS with StartWith and Scan的更多相关文章
- [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 ...
- Angular Multiple HTTP Requests with RxJS
原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [RxJS] Updating Data with Scan
You often need to update the data flowing through the stream with custom logic based on what you nee ...
- [AngularJS] Isolate State Mutations in Angular Components
Managing state is one of the hardest things to do in any application. Angular 2 tackles this problem ...
- RxJS之组合操作符 ( Angular环境 )
一 merge操作符 把多个 Observables 的值混合到一个 Observable 中 import { Component, OnInit } from '@angular/core'; i ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
- [Angular] Refactor Angular Component State Logic into Directives
Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...
随机推荐
- Python os常用模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...
- exp、imp简单测试
imp 分为以下几个测试场景 imp name1/password1 file=xxxx.dmp full=y fromuser=name2 touser=name3 场景1 name1正确.pas ...
- 【转】Objective-C中一种消息处理方法performSelector: withObject:
原文 : http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html Objective-C中调用函数的方法是“消息传递”,这 ...
- NSURLConnection下载
@interface AppDelegate () <NSURLConnectionDataDelegate> { NSMutableData *mData;} @end @impl ...
- js单条新闻向上滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- [转]memmove函数
[FROM MSDN && 百科] 原型: void *memmove( void* dest, const void* src, size_tcount ); #include&l ...
- 实时错误 '91' :对象变量或with块变量未设置
大家这几天在做学生信息管理系统的时候,出现最多的应该就是这个问题了,“实时错误‘91’:对象变量或with块变量未设置”.如右图: 遇到这个问题,我们首先应该去参考MSDN,不过这时候MSDN似乎没有 ...
- AutoMapper DynamicMap天坑
如果调用Mapper.DynamicMap该方法,会将所有映射重置为默认映射,即以字段名匹配映射.
- Python自动化运维之12、面向对象进阶
上一篇<面向对象基础>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...
- C语言基础文件读写操作
整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...