简体   繁体   中英

How to feed an rxjs observer

everyone seems to find observers easy but i find them extremely difficult. I need to create an observable then feed a number to it when a button is pressed, in all the examples that i found they define the content of the observable in the constructor but i need to feed it after is created. Is this even possible?

Thanks

 const { Subject, fromEvent } = rxjs; const number$ = new Subject(); fromEvent(document.getElementById('button'), 'click').subscribe(() => { number$.next(Math.random() * 10); }); number$.subscribe(number => { document.getElementById('output').innerText = number; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.3.3/rxjs.umd.min.js"></script> <button id="button">Click for random number</button> <div id="output"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM