Difference between @Injectable and @Inject decorators
Feature | @Injectable Decorator | @Inject Decorator |
---|---|---|
Purpose | Marks a class as eligible for dependency injection. | Specifies a dependency token for manual injection. |
Usage | Applied to service classes or providers. | Applied to constructor parameters to specify injection dependencies. |
Dependency Scope | Defines the metadata needed for DI resolution. | Specifies the exact dependency to be injected. |
Dependency Type | Marks a class as a candidate for DI resolution. | Directs DI to inject a specific dependency instance. |
Example | @Injectable({ providedIn: ‘root’ }) | constructor(@Inject(MyDependency) private dependency: MyDependency) |
Difference between @Injectable and @Inject Decorators
In Angular, decorators play an important role in defining and managing dependencies within the application. Two commonly used decorators are @Injectable
and @Inject
. While they both relate to dependency injection, they serve different purposes and are applied to different elements within an Angular application.
Table of Content
- What are @Injectable Decorators ?
- What are @Inject Decorators ?
- Difference between @Injectable and @Inject decorators
- Conclusion
Contact Us