Understanding Behavior Subject in Angular

by iFour Technolab Pvt. Ltd. / Friday ,27th November 2020 / Published in Angular, Angular Development, Behavior Subject in Angular

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

The Subject is the special type of observable that allows you to send the data to other components or services. It allows values to multicasted to many Observers.

There are different types of subjects in Angular like async subject, behavior subject, and replay subject. In this blog, we will discuss the behavior subject.

You can import subject from the rxjs library.

Import {Subject} from ‘rxjs’;

Let’s see an example of Subject:

What is BehaviorSubject?

Behavior subject is the most common subject in angular. The Behavior subject represents the current value and it is imported from the Rxjs library. It is similar to the subject but the difference is that we can set the initial value in the behavior subject.

Let’s say we want to store username but we want to change it. We want to store an initial name and also be able to access the current value. You can get the last emitted value from the behavior subject.

Behavior subject will always return a value, even if no data has been emitted from the subscriber.

You can import behavior subject from the ‘rxjs’ or ‘rxjs/BehaviorSubject library’.

Why BehaviorSubject?

Load the data once or when we need the data.

When the data changes then notify the value that is subscribing to the Behavior Subject.

Keep the data updated automatically across the application.

Remove unnecessary requests to load the data just for the cause of ensuring that the data is already loaded.

Let’s see an example of Behavior Subject:

There are few things happening here:

  1. In this example, first we create an object of the behavior subject which simply shows that bsubject is a type of number and its initialized value is 1 .
  2. bsubject has two main methods subscribe and next. Then bsubject emits the value and subscribes the value using the subscribe method.
  3. The subject emits the next value using the next method and the new subscriber will get the latest value.
  4. The bsubject emits the next value and initializes with the value 3 and the new subscriber gets the latest value.

Let’s see another example of Behavior Subject:

Instantiate new behavior subject with the initial value: In this example, the initial value is number (10).

To get the current value, you can use the getValue() method:

You should see an output: Current Value: 10

You can change the value of an existing behavior subject using the next () method with the new value.

After that you can call the getValue() method, then you can acquire the current value.

You should see an output: Current Value: 30

Conclusion

In this blog, we have seen what is subject and how to use the subject in angular. We discussed the behavior subject that is the most common subject in angular. The Behavior subject represents the current value. The Behavior subject is similar to the subject but the difference is that we can set the initial value in the behavior subject.

The Subject is the special type of observable that allows you to send the data to other components or services. It allows values to multicasted to many Observers.

There are different types of subjects in Angular like async subject, behavior subject, and replay subject. In this blog, we will discuss the behavior subject.

You can import subject from the rxjs library.

Import {Subject} from ‘rxjs’;

Let’s see an example of Subject:

What is BehaviorSubject?

Behavior subject is the most common subject in angular. The Behavior subject represents the current value and it is imported from the Rxjs library. It is similar to the subject but the difference is that we can set the initial value in the behavior subject.

Let’s say we want to store username but we want to change it. We want to store an initial name and also be able to access the current value. You can get the last emitted value from the behavior subject.

Behavior subject will always return a value, even if no data has been emitted from the subscriber.

You can import behavior subject from the ‘rxjs’ or ‘rxjs/BehaviorSubject library’.

Why BehaviorSubject?

Load the data once or when we need the data.

When the data changes then notify the value that is subscribing to the Behavior Subject.

Keep the data updated automatically across the application.

Remove unnecessary requests to load the data just for the cause of ensuring that the data is already loaded.

Let’s see an example of Behavior Subject:

There are few things happening here:

  1. In this example, first we create an object of the behavior subject which simply shows that bsubject is a type of number and its initialized value is 1 .
  2. bsubject has two main methods subscribe and next. Then bsubject emits the value and subscribes the value using the subscribe method.
  3. The subject emits the next value using the next method and the new subscriber will get the latest value.
  4. The bsubject emits the next value and initializes with the value 3 and the new subscriber gets the latest value.

Let’s see another example of Behavior Subject:

Instantiate new behavior subject with the initial value: In this example, the initial value is number (10).

To get the current value, you can use the getValue() method:

You should see an output: Current Value: 10

You can change the value of an existing behavior subject using the next () method with the new value.

After that you can call the getValue() method, then you can acquire the current value.

You should see an output: Current Value: 30

Conclusion

In this blog, we have seen what is subject and how to use the subject in angular. We discussed the behavior subject that is the most common subject in angular. The Behavior subject represents the current value. The Behavior subject is similar to the subject but the difference is that we can set the initial value in the behavior subject.

  • Pin It
TOP