源代码:

import { Component, OnInit } from '@angular/core';
import { JerrySandBoxService } from './jerrySandBoxService';
import { GreetingService } from './greeting.service';
import { fromEvent } from 'rxjs';@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {title = 'sandbox';constructor(private jerryService: JerrySandBoxService,private englishGreet: GreetingService){// this.jerryService.print();this.jerryService.print2();console.log(this.englishGreet.greet('Jerry'));}ngOnInit(): void {const button = document.querySelector('button');fromEvent(button, 'click').subscribe(() => {console.log('I am Clicked!');});}jerryTest(){console.log('Hello');}}

在html里定义一个button:

点击之后,看到输出:

可以改得更高级一些:

  ngOnInit(): void {const button = document.querySelector('button');fromEvent(button, 'click').pipe(scan(count => count + 1, 0)).subscribe(count => console.log(`Clicked ${count} times`));}

测试输出:

scan 操作符的工作原理与数组的 reduce 类似。它需要一个暴露给回调函数当参数的初始值。每次回调函数运行后的返回值会作为下次回调函数运行时的参数。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Angular rxjs fromEvent使用的一个例子相关推荐

  1. Angular应用的router-outlet使用一个例子

    项目地址:https://angular-jerry-empty-store.stackblitz.io TopBar Component里,使用Angular routerLink指令设置了一个路由 ...

  2. rxjs 操作符 pairwise 的一个例子

    Groups pairs of consecutive emissions together and emits them as an array of two values. pairwise 将连 ...

  3. Angular 自定义 structural 指令的一个例子

    The <ng-container> is a syntax element recognized by the Angular parser. It's not a directive, ...

  4. Angular元素属性绑定的一个例子

    如下图的例子:a标签的disabled class绑定到model.active属性的取反值,即如果model.active为false,该标签就为disabled状态. 这个a标签页默认是disab ...

  5. Angular list列表绑定的一个例子

    新建一个hero数组作为测试数据: import { Hero } from './heroes/hero';export const HEROES: Hero[] = [{ id: 11, name ...

  6. SAP Spartacus 中,Angular <ng-container>使用的一个例子

    Angular官网里对ng-container的介绍: The Angular is a grouping element that doesn't interfere with styles or ...

  7. Angular component的一个例子

    官网:https://angular.io/guide/architecture-components Before a view is displayed, Angular evaluates th ...

  8. 通过rxjs的一个例子, 来学习SwitchMap的使用方法

    switchMap相关文章 rxjs里switchMap operators的用法 通过rxjs的一个例子, 来学习SwitchMap的使用方法 rxjs switchMap的实现原理 rxjs的ma ...

  9. Webpack入门——使用Webpack打包Angular项目的一个例子

    (一)什么是Webpack Webpack是一个前端的模块管理工具(module bundler),以下是webpack的官网:http://webpack.github.io/,一进入官网可以看到下 ...

最新文章

  1. leetcode算法题--构建乘积数组
  2. CAD计算机辅助设计与BIM的区别,3D建模、CAD和BIM三者之间的差异是什么,有什么区别?...
  3. How to Secure Your Smart Contracts: 6 Solidity Vulnerabilities and how to avoid them (Part 2)
  4. Quartus和ISErom文件格式
  5. OPENCV-7 学习笔记
  6. 毕业论文管理系统(类图,er图,时序图)
  7. LINUX SHELL参数连接
  8. 计算机键盘和指法练习实验报告模板,实验报告总结怎么写
  9. JAVA 实现图片原比例无损压缩
  10. 宽带IPTV单线复用
  11. 打印六芒星_java
  12. 【PANDA教程】PANDA详细教程笔记
  13. Client requested master to start replication from impossible position; the last event was read from
  14. Python(jupyter notebook)--K-means聚类实例
  15. linux文件无法写入
  16. 阿里巴巴内推电话面试
  17. Unity Shader - Billboard 广告板/广告牌 - BB树,BB投影
  18. 小程序mpvue图片绘制水印_mpvue开发小程序如何使用腾讯官方小程序插件--地图
  19. 十五铬钼钢板和十二铬一钼钒钢板的区别
  20. OBIEE 报错 ODBC 驱动程序返回了错误 (SQLNumResultCols)

热门文章

  1. java ftpclient API上传下载文件,不支持中文
  2. Java编程思想之-匿名内部类
  3. Knockout中ViewModel与Model的互转
  4. oracle 创建触发器_oracle创建触发器
  5. 关于Vue中计算属性computed和methods属性的区别,你了解多少呢
  6. Redis Cluster集群架构实现
  7. CS229 6.5 Neurons Networks Implements of Sparse Autoencoder
  8. Mybatis的下载并搭建核心架构
  9. Nginx特性验证-反向代理/负载均衡/页面缓存/URL重定向
  10. MAC地址表配置与绑定