Subject vs BehaviorSubject in Angular


I am passing data between 2 independent components using shared service. When I used a subject in my shared service, I could not see the subscribed data in html of the subscribed component but could see it in the console after subscribing.
Whereas if I use Behaviorsubject, it worked fine.
Can anyone explain me the reason for this.


  //code with subject (not working)
  private msgStatistics = new Subject<any>();
      msgStatistics$ = this.msgStatistics.asObservable();
      msgStats(message) {

    //code with BehaviorSubject (working)
    private msgStatistics = new BehaviorSubject<Object>(null);
      msgStatistics$ = this.msgStatistics.asObservable();
      msgStats(message) {


this.shared_service.msgStatistics$.subscribe(res => {
        this.message = res

the above console is printing the value of message in both the cases but it is not rendering in html for subject.

Source: Angular Questions

Categorized as angular, behaviorsubject, subject-observer Tagged , ,


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us