Module 03-01: RxJS Observables
February 14, 2023 ยท View on GitHub
DEMOS
- StackBlitz - RxJS Observables
Training
Training 1: Using Observable creation functions to create an Observable
- Create a new Angular component, let's call it
UserComponent. - In the "UserComponent", import the necessary RxJS functions:
of,from,interval, andfromEvent. - In the component's class, create a method called
createObservable()that returns an Observable of your choice. For example, you could create an Observable using the of function that emits an array of user objects. - In the component's template, use the
asyncpipe to subscribe to the Observable returned fromcreateObservable()and display the emitted values in the UI. - Add a button to the template and use the
fromEventfunction to create an Observable that emits a click event every time the button is clicked. - Use the interval function to create an Observable that emits a number every second, and use the
takeUntiloperator to stop the emission of values when the button from previous step is clicked. - Finally, use the
fromfunction to create an Observable from a promise that resolves to a user object, and use the merge operator to merge this Observable with the Observable from step 6. Use theasyncpipe to display the emitted values in the UI.