This post lists some subjective questions and answers related to the Dependency Injection mechanism of Angular. This collection contains some potential interview questions that may be helpful if you are preparing for a Frontend Engineering job interview specific to Angular framework. Check out Angular Interview Series – Topic Wise Q&A.
1. What is a service in Angular?
A service is a class that encapsulates reusable business logic or data access, designed to be injected into components or other services.
2. How do you create a service in Angular?
We can create a service using the Angular CLI command:
ng generate service service-name
OR
ng g s service-name3. What is dependency injection (DI) in Angular?
Dependency injection is a design pattern that allows a class to receive its dependencies from an external source rather than creating them itself, promoting better code organization and testability.
4. How do you provide a service in Angular?
A service can be provided in the @Injectable decorator by setting providedIn: 'root', or it can be added to the providers array in a module or component.
5. What is the purpose of the @Injectable() decorator?
The @Injectable() decorator marks a class as available to be injected as a dependency and can also specify how the service is provided.
6. What is the difference between singleton and instance services in Angular?
A singleton service is created once and shared across the application, while an instance service is created anew for each component or module that requests it.
7. How do you inject a service into a component?
We can inject a service into a component by declaring it in the component’s constructor, e.g., constructor(private myService: MyService) {}.
8. What is the role of the injector in Angular?
The injector is a service container that creates and provides instances of services and manages their lifecycle.
9. How can you inject a service into another service?
Yes, We can inject one service into another by including it in the constructor of the service class.
10. What is hierarchical injection in Angular?
Hierarchical injection allows services to be provided at different levels (root, module, or component), enabling different instances of a service based on where it is provided.
These are a few questionnaires specific to Angular Modules. I will be adding more questions and answers to this post. To know more please refer to the Official documentation on Dependency Injection in Angular.
