[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 of your stream.

import {Component} from ‘angular2/core‘;
import {bootstrap} from ‘angular2/platform/browser‘;
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();
    public clock;
    constructor(){
        this.clock = this.click$.map( () => new Date());
    }
}

bootstrap(App);

So here create a click$ subject, every time you click the button, it will map to a current date value.

时间: 2024-12-07 19:19:41

[Angular 2] Handling Click Events with Subjects的相关文章

[Cycle.js] Read effects from the DOM: click events

So far we only had effects that write something to the external world, we are not yet reading anything from the external world into our app. This lesson shows how we can change the DOM Driver to return a "DOM Source" representing read effects, s

Google C++单元测试框架---Google TestExtending Google Test by Handling Test Events

Google TestExtending Google Test by Handling Test Events Google测试提供了一个事件侦听器API,让您接收有关测试程序进度和测试失败的通知. 可以监听的事件包括测试程序的开始和结束,测试用例或测试方法等. 您可以使用此API来扩充或替换标准控制台输出,替换XML输出,或提供完全不同的输出形式,例如GUI或数据库. 例如,您还可以使用测试事件作为检查点来实现资源泄漏检查器. 一.定义事件侦听器 要定义一个事件监听器,你需要继承testin

Button&#39;s four click events

第一种:内部类的方式 1 package com.example.phonedialer; 2 3 import com.example.click2.R; 4 5 import android.net.Uri; 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.view.Menu; 10 import android.view.

[Angular Directive] 3. Handle Events with Angular 2 Directives

A @Directive can also listen to events on their host element using @HostListener. This allows you to add behaviors that react to user input and update or modify properties on the element without having to create a custom component. import {Directive,

[Angular] Subscribing to router events

In our root component, one thing we can do is subscribe to Router events, and do something related to router events. So how can we get router event? export class AppComponent implements OnInit { constructor(private router: Router) {} ngOnInit() { thi

[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 cloc

angular/ionic自定义click

项目既要用于PC网页,又要在iPad之类的移动设备上使用. 在iPad上的这种大屏设备上用户的手指点击按钮时,可能会发生一个细微的位移导致不能触发click事件. 会给用户一种不太灵敏的感觉,为解决此问题我自己写一个指令代替click. 贴上代码: @Directive({ selector: '[yun-click]' }) export class YunClickDirective implements OnInit, OnDestroy { @Output('yun-click') ev

Android 布局(Layout)指南

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Android 官方文档 布局 相关资源链接汇总如下: android-sdk-macosx-4.4.2/docs/guide/topics/ui

[Angular 2] Using events and refs

This lesson shows you how set listen for click events using the (click) syntax. It also covers getting values off of an input using the #ref syntax then passing that value into the onClick event handler.