JavaScript Edu Resources. Angular Courses. React Tutorials. Join us!

Dependency Injection

Angular 1.x has two APIs for injecting dependencies into a directive. Angular 2 unifies the two APIs, making the code easier to understand and test.

  • The dependency resolution algorithm works as follows:

    So in the following example

    DI will start resolving in the same injector where the binding is defined.

  • For instance, the decorator tells DI to look for only in the same injector where is defined.
  • The decorator tells DI to look for in the whole tree starting from the parent injector.
  • An injector has only one instance created by each registered binding.
  • We can create a child injector for each new instance:

    Or we can register a factory function:

awesome-angular/ at gh-pages · AngularClass/awesome-angular · GitHub

Comments are closed, but trackbacks and pingbacks are open.