days
-4
-4
hours
-1
-7
minutes
-1
0
seconds
-4
-5
search
Angular injection

Angular 1.x vs. Angular 2.0 – Unified dependency injection

Victor Savkin
Angular structure via Shutterstock

The next major release of Angular is bringing a unified method for injecting dependencies into a directive.

Angular 1.x has two APIs for injecting dependencies into a directive. These APIs are not interchangeable, so depending on what you are injecting, you need to use one or the other. Angular 2 unifies the two APIs, making the code easier to understand and test.

Angular 1.x

Let’s start with a simple example: a directive autocompleting the user input.

<input name="title" autocomplete="movie-title">

The autocomplete directive takes the user input, and using the autocompleter service object, gets matching movie titles from the backend.

    module.directive('autocomplete', ["autocompleter", function(autocompleter) {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          //...
        }
      }
    }]);

Note, we have to use two mechanisms to inject the autocompleter and the element.

  • The autocompleter service is injected into the directive factory function, and it is done by name.
  • The element and the attributes are injected into the link function. This is done by position, which forces us to pass in scope, even though we may not need it.

Angular 2

Now, contrast it with the Angular 2 way of defining the same directive.

    @Decorator({selector: '[autocomplete]'})
    class Autocomplete {
        constructor(autocompleter:Autocompleter, el:NgElement, attrs:NgAttributes){
            //...
        }
    }

Or if you prefer ES5

    function Aucotomplete(autocompleter, el, attrs) {
        //...
    }
    Aucotomplete.annotations = [new Decorator({selector: '[autocomplete]'})];
    Aucotomplete.parameters = [[Autocompleter], [NgElement], [NgAttributes]];

In Angular 2 the autocompleter, the element, and the attributes are injected into the constructor by name.

Hierarchical injectors

How is it possible? Should not every instance of the directive get its own element? It should. To enable that the framework builds a tree of injectors that matches the DOM.

    <div>
        <input name="title" autocomplete="movie-title">
        <input name="actor" autocomplete="actor-name">
    </div>

The matching injector tree:

    Injector Matching Div
        |
        |__Injector Matching Movie Title
        |
        |__Injector Matching Actor Name

Since there is an injector for every DOM element, the framework can provide element-specific information, such as an element, attributes, or a shadow root.

SEE ALSO: A sneak peak at Angular 2.0

Summary

In Angular 2 there is a single way to inject dependencies into a directive, regardless of what you inject: user-defined services, framework services, elements, attributes, or other directives.

This article was originally published on victorsavkin.com

Author
Victor Savkin
Victor Savkin works on AngularJS at Google.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of