As I dive into Angular, I find myself tangled in the concepts of observables, observers, and subscriptions. Could you shed some light on these for me?
As I dive into Angular, I find myself tangled in the concepts of observables, observers, and subscriptions. Could you shed some light on these for me?
Let's take a look at this easy-to-understand visual illustration:
https://i.stack.imgur.com/vvHEc.png
As shown in the diagram, an Observable serves as a continuous stream of events or data. In Angular, observables are commonly returned from methods like http.get
and myinputBox.valueChanges
.
Subscribing to an observable is what initiates the stream. Without subscribing (or using an async pipe), the values will not be emitted. It's similar to subscribing to receive newspapers or magazines - you won't start receiving them until you subscribe.
When you subscribe to an observable, you provide an observer. This observer includes three essential methods:
A method to handle each emission from the observable.
A method to manage any errors that occur.
A method for cleaning up tasks when the observer completes. This cleanup method is rarely needed when dealing with Angular's observables.
(I completely agree - navigating through documentation can feel like searching for the forest among trees. It's reassuring to know that efforts are being made to enhance the clarity of the docs.)
Let's break it down with a simple analogy:-
Observable can be compared to a TV channel that broadcasts new shows. (( It provides the content(data) for you to watch, makes it a data source for you))
Your TV screen represents an Observer
Your TV screen (Observer) will only show you notifications about new shows or special events on a specific TV channel (Observable) if you are tuned in to that channel
(Observer tunes into Observable to receive new data/any event)
where observable serves as the content provider, tuning in is like a method/function, and Observer is typically on your end
An Observable can be likened to a variety of data sources, such as user inputs or HTTP requests.
In this example, we are creating our own custom observable.
var observable = Observable.create((observer: any)=>{
observer.next("Hello")
observer.next("how are you")
observer.complete()
observer.next("This will not be sent to the observer")
});
An Observer is someone who subscribes to the Observable.
observable.subscribe(
(data: any) => console.log(data), // handle data
(error: any) => console.log(error), // handle errors
() => console.log('completed'); // handle completion
You can learn more about Observables here: http://reactivex.io/documentation/observable.html
Highlighted below are some key points:
1) Explanation: The "Observable and Observer" concept involves the passing of messages from a publisher to a subscriber.
2) Functionality flow:
An Observable is created
The Observer subscribes to the Observable
3) Practical applications of Observable and Observer
Currently working with the angular2-webpack-starter technology and utilizing VSCode along with Chrome debugger. After numerous attempts, I was able to successfully set a breakpoint, but it appears that the line mapping is incorrect. The issue persists in ...
Struggling to style the header of a datatable in my Angular project using PrimeNG components. Despite trying various solutions, I am unable to override the existing styles. Even attempting to implement the solution from this PrimeNG CSS styling question o ...
Is it possible to listen to an event triggered in AngularJS using regular JS (outside of Angular)? I have a scenario where an event is being emitted using RxJS in Angular 2. Can I observe that event from pure JS? Here's some example pseudo code: imp ...
I am currently facing an issue with my nodejs server that uses the next() function to catch errors. The problem is that the thrown error is being returned to the frontend in HTML format instead of JSON. I need help in changing it to JSON. Here is a snippe ...
I have successfully implemented an Angular search function on my project. You can find it here to allow users to search for courses. The search function uses a read-more directive that displays a preview of the description and keywords before allowing use ...
Looking to list all children of "Requests" from my firebase realtime database in a structured format. Here's a snapshot of how the database is organized: https://i.stack.imgur.com/5fKQP.png I have successfully fetched the data from Firebase as JSON: ...
Currently I am attempting to integrate Aframe into my angular 2 project. Although I have imported the library in my index.html file, I am still encountering difficulties using the aframe directive. For instance: <a-scene> <a-box color="red"& ...
Whenever I try to change the appearance of the form field to outline, the floating label ends up breaking the outline. Here is a code snippet for reference: <mat-form-field appearance="outline"> <mat-label>Password&l ...
Exploring options for improving this code: This is currently how I handle the observable data: this.observable$.pipe(take(1)).subscribe((observableValue) => { this.behaviourSubject$.next(observableValue); }); When I say improve, I mean finding a wa ...
Many of us are familiar with the trick of "storing the conditional variable in a variable" using *ngIf="assertType(item) as renamedItem" to assign a type to a variable. This technique has always been quite useful for me, as shown in this example: <ng-t ...
I've recently implemented the [Datepicker feature from Telerik Kendo UI][1] While the datepicker functions perfectly, I have encountered a single issue where it displays undefined when the date value is not defined: [![enter image description here][ ...
Hey there, I am receiving responses from 2 different API calls. Initially, I make a call to the first API and get the following response: The first response retrieved from the initial API call is as follows: dataName = [ { "id": "1", ...
My Angular code is attempting to hide columns of a table using the function shouldHideColumn(). Despite my efforts, I am unable to bind my tags to the <th> and <td> elements. An error keeps popping up saying Can't bind to 'printerColu ...
I developed a unique Angular component resembling a tree structure. The design includes multiple branches and nodes in alternating colors, with the selected node marked by a blue dot. https://i.stack.imgur.com/fChWu.png Key features to note: The tree&ap ...
I am currently working on implementing two intersecting drag and drop functionalities using cdkDragDrop. Although I generally try to avoid using tables, I have created one in this case for the sake of easier explanation. Here is the link to StackBlitz: ht ...
After reviewing my code, I have encountered the following issues: In my shared.actions.ts file: import { Action } from '@ngrx/store'; import { Error } from '../error.interface'; export const types = { IS_LOADING: '[SHARED] IS_L ...
I tried using console.log in both the constructor and ngOnInit() of Resolver but for some reason, they are not being logged. resolve:{serverResolver:ServerResolverDynamicDataService}}, console.log("ServerResolverDynamicDataService constructor"); console ...
I am working on my AngularJS 1.5 application where I have controllers directly calling service functions. What is the recommended approach to achieve this in Angular? $scope.permissions = ClockingMenuService.permissions; $scope.data = ClockingMenuService ...
I am new to working with Angular and HTML. Currently, I have two different components named componentA and componentB, each having their own .html, .css, and .ts files. In the componentA.css file, I have defined some styles like: .compA-style { font- ...
We integrated ng-apimock into our Angular project and successfully created mock definitions and wrote tests using protractor. Now we are looking to implement global variables in our mock definitions. Here is an example of a mock definition: { "expressi ...