Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Dependency Injection (DI) correctly in Angular2?

Tags:

I have been trying to figure out how the (DI) Dependency Injection work in Angular2. I ran into lots of problem/issue every time when I tried to Inject a service/or class into my components.

From different googled articles, I need to either use providers: [] in the Component configuration, or sometimes I need to use @Inject() in my constructor or inject directly in the bootstrap(app, [service])? I've also seen some articles want me to put @injectable decorator.

For example: to inject Http, I only need to import{Http} and put Http in the providers, but for FormBuilder, I need to use @Inject() in constructor.

Is there any rule of thumb for when to use what? Could you please provide some example code snippet? Thank you :-)

like image 893
George Huang Avatar asked Mar 20 '16 02:03

George Huang


People also ask

What is the right way to inject dependency?

Constructor injection should be the main way that you do dependency injection. It's simple: A class needs something and thus asks for it before it can even be constructed. By using the guard pattern, you can use the class with confidence, knowing that the field variable storing that dependency will be a valid instance.

How do you implement DI?

The recommended way to implement DI is, you should use DI containers. If you compose an application without a DI CONTAINER, it is like a POOR MAN'S DI . If you want to implement DI within your ASP.NET MVC application using a DI container, please do refer to Dependency Injection in ASP.NET MVC using Unity IoC Container.

What is DI and how Angular handling DI?

Dependency injection, or DI, is one of the fundamental concepts in Angular. DI is wired into the Angular framework and allows classes with Angular decorators, such as Components, Directives, Pipes, and Injectables, to configure dependencies that they need.

How is DI achieved in spring boot?

Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container “injects” objects into other objects or “dependencies”. Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container.


1 Answers

Broad question, TL;DR version


@Injectable()

  • is a decorator which tells the typescript that decorated class has dependencies and does not mean that this class can be injected in some other.

  • And then TypeScript understands that it needs to Inject the required metadata into decorated class when constructing, by using the imported dependencies.

bootstrap(app, [service])

  • bootstrap() takes care of creating a root injector for our application when it’s bootstrapped. It takes a list of providers as second argument which will be passed straight to the injector when it is created.

  • You bootstrap your application with the services that are gonna be used in many places like Http, which also means you'll not need to write providers: [Http] in your class configuration.

providers: [service]

  • providers also does the work of passing all the services' arguments to Injector .

  • You put services in providers if it's not bootstrap()ped with. And is needed only in a few places.

@Inject()

  • is also a decorator a function that does the work of actually injecting those services
    like this. constructor(@Inject(NameService) nameService)
  • but if you use TS all you need to do is this constructor(nameService: NameService) and typescript will handle the rest.

Further Reading

  • If you want to dig deep in DI, take a look at this amazing article

  • and to understand Decorators vs Annotations see this.

  • Here is the official guide.

  • Gunter's Answer Mark Rajcok's Answer and Accepted Answer

Hope this helps. :)

like image 58
Ankit Singh Avatar answered Sep 18 '22 19:09

Ankit Singh