Dependency Injection - sravanthimendu98/angular GitHub Wiki

Dependency Injection (DI)

It is a design pattern used to improve code modularity and testing by allowing a class to receive its dependencies from external sources rather than creating them itself. This is achieved through the Angular Injector, which is responsible for creating and managing the dependencies.

image

@Injectable({ providedIn: 'root' })

Explanation: The @Injectable decorator marks the class as a service that can be injected into other components or services.

providedIn: 'root': This ensures that the service is available as a singleton throughout the entire application without needing to be declared in a specific module. This makes it available wherever it's injected.

  1. private formDataSubject = new BehaviorSubject(null);

BehaviorSubject: A BehaviorSubject is a type of Observable from RxJS that not only emits new values to its subscribers but also holds the current value. This means any component subscribing to it will immediately receive the latest value upon subscription. Initial Value (null): The BehaviorSubject is initialized with a default value of null. This means that until a new value is provided, the service will return null to any subscribers.

⚠️ **GitHub.com Fallback** ⚠️