zoukankan      html  css  js  c++  java
  • [Angular 2] Passing Template Input Values to Reducers

    Angular 2 allows you to pass values from inputs simply by referencing them in the template and passing them into your Subject.next() call. This lesson shows you how to make a number input and pass the value so you can configure how much you want the clock to change.

    /**
     * Created by wanzhen on 26.4.2016.
     */
    import {Component} from 'angular2/core';
    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/observable/interval';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/observable/merge';
    import 'rxjs/add/operator/startWith';
    import 'rxjs/add/operator/scan';
    import 'rxjs/add/operator/mapTo';
    import {Subject} from "rxjs/Subject";
    import {Store} from '@ngrx/store';
    import {SECOND, HOUR} from './reducer';
    
    @Component({
        selector: 'app',
        template: `
            <input #inputNum type="number" value="0">
            <button (click)="click$.next(inputNum.value)">Update</button>
            <h1>{{clock | async | date:'yMMMMEEEEdjms'}}</h1>
            `
    })
    export class App {
        click$ = new Subject()
                .map( (number) => ({type: HOUR, payload: parseInt(number)}));
    
        seconds$ = Observable.interval(1000)
            .mapTo({type: SECOND, payload: 1});
    
        clock;
    
        constructor(store:Store) {
            this.clock = store.select('clock');
    
    
            Observable.merge(
                this.click$,
                this.seconds$
                )
                .subscribe((action)=>{
                    store.dispatch(action)
                })
        }
    }
  • 相关阅读:
    【2021-08-09】问题还需一点一点去改正
    【2021-08-08】连岳摘抄
    【2021-08-07】请教帖
    21春助教总结
    实践总结+技术博客评分
    来吧 ,来吧 自己搭建一个erp 系统
    博客索引
    「CCNU21暑期第六次周赛」
    「CCNU21暑期第五次周赛」
    「图论」连通性问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5437352.html
Copyright © 2011-2022 走看看