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:
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’.
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:
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
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.
July 29, 2021
July 22, 2021
July 20, 2021
July 16, 2021
Well do everything we can to make our next best project!
Check out our most recent blogs
July 29, 2021
What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...
July 22, 2021
What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...
July 20, 2021
What is binding in Angular? Binding in angular apps is the automatic synchronization of data within the model and view components. You can use data binding to define things...
Well do everything we can to make our next best project!