I'm quite new to Angular and from what I can tell Subject is the standard class used for multicasting. When trying out this class I've found that there are two (probably even more) avenues of dealing with changes in its value.
Using Observable type objects within the component directly
In this approach an Observable is declared within the component like so:
foo$ : Observable<boolean>;
and is then used in the html file using the following method:
<p *ngIf="(foo$ | async) as foo">Bar!</p>
The second approach is to have a Subscription type object within the component which assigns to some member variable:
s: Subscription;
foo: boolean;
Where the subscription is initialized as follows:
constructor(private fbs: FooBarService) {
this.s = fbs.fooObservable.subscribe(v => this.foo = v);
// this.s.unsubscribe() is called within ngOnDestroy()
}
The html would then use the code like this:
<p *ngIf="foo">Bar!</p>
Is there any reason, other than personal preference, to prefer either one of these approaches?
Is there any reason, other than personal preference, to prefer either one of these approaches?
These kinds of questions are off topic here, but there is value to the community in giving a general answer. There is enough distinction between the two that it should be talked about.
One approach is called a reactive component and the other is a stateful component.
The view handles presentation of data from observables using the async pipe. If a component only uses observables and async pipe for presentation, then the component is stateless and reacts to changes automatically via the view. This helps create a drier feel for the templates.
This approach has the following advantages.
OnPush change notification a lot easier.This approach has the following disadvantages.
data.subscribe(value => this.value = value) when they don't understand reactive programming.mergeMap() instead of switchMap() as an example.<ng-container *ngIf="data$ | async as data"> will create a view variable data that is of an unknown type in most IDEs.debugger; because the component has no state to debug.A component is stateful when it has properties that are used in the view template. The internal state of the component must be changed in order to represent a change in the view, and this is the default type of component in Angular.
This approach has the following advantages.
@Input() bindings are stateful to begin with.debugger; in the browser, because you can see the current state of the component.This approach has the following disadvantages.
subscribe() calls when you mix in observables.data.subscribe(value => this.data = value).When deciding which of the two approaches to use. I recommend starting with stateful components but progress your skills towards the reactive components.
From my experience, reactive components are the way to go because they are destinations for observable streams. These are components that bring together observables to create a responsive view of that data, and they react automatically to changes in those streams. At the sametime, merging of data as a destination is more of an architectural design in Angular. So it's a broader discussion and topic, but keep learning and you'll get there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With