Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - Converting events to observable streams

Tags:

angular

rxjs

I'm fairly new to Rxjs and Angular 4 and can't get my head around how to do this.

Template:

<div class="btn-group cp">
   <input [colorPicker]="bgColor" 
   (colorPickerChange)="colorChanged()" 
   [style.background]="bgColor">
</div>

colorChanged() gets called by the colorPickerChnage on mousemove. I want to have something like this in my component:

colorChanged$.subscribe(data => console.log(data))

(It will have some debounce and more subscribers, that's why I'd like to have it as an observable.)

like image 500
wood1y Avatar asked Apr 06 '17 17:04

wood1y


1 Answers

I solved it.

Template:

<div class="btn-group cp">
   <input [colorPicker]="bgColor" 
   (colorPickerChange)="colorChanged(bgColor)" 
   [style.background]="bgColor">
</div>

And then in the component:

import { Subject } from 'rxjs';

  colorChanged$ = new Subject<string>();

  colorChanged(bgColor) {
     this.colorChanged$.next(bgColor)
  }

  ngOnInit() {
     this.colorChanged$.subscribe(v => console.log(v));
  }
like image 57
wood1y Avatar answered Sep 27 '22 21:09

wood1y