• Stars
    star
    137
  • Rank 257,073 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🦊 ViewChild and FromEvent β€” a Match Made in Angular Heaven

MIT commitizen PRs styled with prettier All Contributors ngneat

ViewChild and FromEvent β€” a Match Made in Angular Heaven

Turn ViewChild and ContentChild queries into an RxJS stream.

Installation

npm install @ngneat/from-event

Usage

Use the FromEvent decorator with ViewChild or ContentChild. Note that it expects to get ElementRef.

import { FromEvent } from '@ngneat/from-event';

@Component({
  selector: 'my-btn',
  template: `
    <button #button>
      <ng-content></ng-content>
    </button>
  `
})
class ButtonComponent implements AfterViewInit {
  @FromEvent('click')
  @ViewChild('button') 
  click$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.click$.subscribe(console.log);
  }
}

You are not limited to use it only inside AfterViewInit or AfterContentInit:

@Component({
  template: `
    <button #plus>+1</button>
    <button #minus>-1</button>
    <button #reset>Reset</button>
    {{ counter$ | async }}
  `,
})
export class CounterComponent {
  @FromEvent('click')
  @ViewChild('plus')
  plus$: Observable<MouseEvent>;

  @FromEvent('click')
  @ViewChild('minus')
  minus$: Observable<MouseEvent>;

  @FromEvent('click')
  @ViewChild('reset')
  reset$: Observable<MouseEvent>;

  count$ = merge(
    this.plus$.pipe(mapTo(1)), 
    this.minus$.pipe(mapTo(-1))
  ).pipe(
    startWith(0),
    scan((acc, curr) => acc + curr)
  );

  counter$ = this.reset$.pipe(
    startWith(null),
    switchMap(() => this.count$)
  );
}

A common example is using it with switchMap():

import { FromEvent } from '@ngneat/from-event';

@Component({
  selector: 'my-comp',
  template: `
    <button #trigger>Trigger</button>
  `
})
class MyComponent implements AfterViewInit {
  @FromEvent('click')
  @ViewChild('trigger') 
  trigger$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.trigger$.pipe(switchMap(() => service.doSomething())).subscribe(result => {
      // Do something with the result
    });
  }
}

Use the FromEvents decorator with ViewChildren or ContentChildren. Note that it expects to get ElementRef.

import { FromEvents } from '@ngneat/from-event';

@Component({
  template: `
    <my-btn id="1">Click One</my-btn>
    <my-btn id="2">Click Two</my-btn>
    <my-btn id="3">Click Three</my-btn>
  `
})
class HostComponent implements AfterViewInit, OnDestroy {
  @FromEvents('click')
  @ViewChildren(ButtonComponent, { read: ElementRef }) 
  clicks$: Observable<MouseEvent>;

  ngAfterViewInit() {
    this.clicks$.subscribe(e => console.log(e.target));
  }
}

Have fun, and don't forget to unsubscribe. If you work with Ivy, you can do it with until-destroy.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

πŸ’»

Toni Villena

πŸ’» πŸ’‘

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

falso

All the Fake Data for All Your Real Needs πŸ™‚
TypeScript
3,098
star
2

spectator

🦊 πŸš€ A Powerful Tool to Simplify Your Angular Tests
TypeScript
2,029
star
3

transloco

πŸš€ 😍 The internationalization (i18n) library for Angular
TypeScript
1,856
star
4

until-destroy

🦊 RxJS operator that unsubscribe from observables on destroy
TypeScript
1,712
star
5

elf

πŸ§™β€β™€οΈ A Reactive Store with Magical Powers
TypeScript
1,527
star
6

content-loader

βšͺ️ SVG component to create placeholder loading, like Facebook cards loading.
TypeScript
733
star
7

hot-toast

🍞 Smoking hot Notifications for Angular. Lightweight, customizable and beautiful by default.
TypeScript
687
star
8

cashew

🐿 A flexible and straightforward library that caches HTTP requests in Angular
TypeScript
671
star
9

reactive-forms

(Angular Reactive) Forms with Benefits πŸ˜‰
TypeScript
610
star
10

tailwind

πŸ”₯ A schematic that adds Tailwind CSS to Angular applications
TypeScript
608
star
11

forms-manager

πŸ¦„ The Foundation for Proper Form Management in Angular
TypeScript
517
star
12

query

πŸš€ Powerful asynchronous state management, server-state utilities and data fetching for Angular Applications
TypeScript
510
star
13

error-tailor

πŸ¦„ Making sure your tailor-made error solution is seamless!
TypeScript
478
star
14

helipopper

🚁 A Powerful Tooltip and Popover for Angular Applications
TypeScript
392
star
15

nx-serverless

πŸš€ The Ultimate Monorepo Starter for Node.js Serverless Applications
TypeScript
388
star
16

dialog

πŸ‘» A simple to use, highly customizable, and powerful modal for Angular Applications
TypeScript
371
star
17

hotkeys

πŸ€– A declarative library for handling hotkeys in Angular applications
TypeScript
325
star
18

edit-in-place

A flexible and unopinionated edit in place library for Angular applications
TypeScript
252
star
19

svg-icon

πŸ‘» A lightweight library that makes it easier to use SVG icons in your Angular Application
TypeScript
251
star
20

inspector

πŸ•΅οΈ An angular library that lets you inspect and change Angular component properties
TypeScript
218
star
21

dirty-check-forms

🐬Detect Unsaved Changes in Angular Forms
TypeScript
199
star
22

input-mask

🎭 @ngneat/input-mask is an angular library that creates an input mask
TypeScript
199
star
23

lib

πŸ€– Lets you focus on the stuff that matters
TypeScript
180
star
24

transloco-keys-manager

πŸ¦„ The Key to a Better Translation Experience
TypeScript
174
star
25

dag

🐠 An Angular service for managing directed acyclic graphs
TypeScript
153
star
26

bind-query-params

Sync URL Query Params with Angular Form Controls
TypeScript
147
star
27

overview

πŸ€– A collection of tools to make your Angular views more modular, scalable, and maintainable
TypeScript
104
star
28

aim

Angular Inline Module Schematics
TypeScript
97
star
29

cmdk

Fast, composable, unstyled command menu for Angular. Directly inspired from pacocoursey/cmdk
TypeScript
91
star
30

copy-to-clipboard

βœ‚οΈ Modern copy to clipboard. No Flash.
TypeScript
78
star
31

variabless

JS & CSS - A Match Made in Heaven πŸ’Ž
HTML
78
star
32

loadoff

🀯 When it comes to loaders, take a load off your mind...
TypeScript
78
star
33

effects

πŸͺ„ A framework-agnostic RxJS effects implementation
TypeScript
59
star
34

avvvatars

Beautifully crafted unique avatar placeholder for your next angular project.
TypeScript
42
star
35

react-rxjs

πŸ”Œ "Plug and play" for Observables in React Apps!
TypeScript
37
star
36

subscribe

Subscription Handling Directive
TypeScript
34
star
37

elf-ng-router-store

Bindings to connect Angular router to Elf
TypeScript
24
star
38

ng-standalone-nx

TypeScript
24
star
39

lit-file-generator

🎁 A lit generator for a component, directive, and controller.
JavaScript
19
star
40

storage

TypeScript
18
star
41

material-schematics

TypeScript
3
star
42

svg-icon-demo

TypeScript
1
star