2

I have a doubt, I the project I'm working on, I'v seen this:

private _isReady$: BehaviorSubject<boolean> = new <boolean>(false);
get IsReady$(): Observable<boolean> {
    return this._isReady$.asObservable();
}

And then, In the html template:

{{ isReady$ | async}}

Why not define the behaviorSubject as public, and use it direcly in the html with the async pipe? What is the difference/benefit returning that behaviour subject as an observable to use it in the html?

1 Answer 1

1

The reason being BehaviorSubject(_isReady$) private is that only a particular service should be allowed to emit the observable (no other components or service should be allowed to do so).

But others can only subscribe to it using a publicly accessible Observable IsReady$

Sign up to request clarification or add additional context in comments.

3 Comments

You are right, thank you very much for the explanation
and why is that? why shouldnt every component have the ability to .next into the Subject?
It depends on your use case, ofcourse you are allowed to emit from anywhere. The full responsibility of notifying its subscriber is taken by the service in this case. It could be possible that _isReady value is determined based on the logic that is present in the same service.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.