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 Autocomplete(autocompleter, el, attrs) {
//...
}
Autocomplete.annotations = [new Decorator({selector: '[autocomplete]'})];
Autocomplete.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.

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.

    • paper writing service • 5 months ago

      Trying to find top amount structure publishing business on the net and your web site is extremely realistic if you ask me. I am just wishing this website supports every person a lot. being far more idea across the publishing companies...

    • 1
    • Reply
    • Share ›
    •  
       
    •  
       
      Robert Penner • a month ago

      Looks like a typo: Aucotomplete

    • Reply
    • Share ›
    •  
       
    •  
       
      Serg de Adelantado • 2 months ago

      Hi!
      You wrote: "To enable that the framework builds a tree of injectors that matches the DOM."
      Does it mean that every time we making changes in a DOM(for example, with ng-if, or dynamic ng-include), it will lead to creation of a new Injector tree or re-scan of injections?

    • Reply
    • Share ›
    •  
       
      •  
         
        Victor Savkin Mod Serg de Adelantado • 2 months ago

        This is a good question.

        It works as follows:

        * In Angular2 the view is a chunk of DOM that can be added or removed.
        * The view has a tree of injectors associated with it.
        * We create prototypes for Views and Injectors. This happens during the compilation phase (once per component).
        * NgIf contains either a single View or nothing.
        * When NgIf evaluates to true, we use the prototypes to very efficiently create the required view and injectors.
        * In addition, we also have a view pool that allows us to reuse views and injectors. This is done to reduce GC pressure.

        The answer is:

        The mental model is that we do create the tree every time. But behind the scenes we use optimizations to make it cheap.

      • Reply
      • Share ›
      •  
         
    •  
       
      Sekib Omazic • 5 months ago

      Cool. Do you have a small app showing all this stuff? I'd like to play with it, but example in ng2 repo (todo app) shows not much.

    • Reply
    • Share ›
    •  
       

ANGULAR 2 BITS: UNIFIED DEPENDENCY INJECTION的更多相关文章

  1. [Angular] Communicate Between Components Using Angular Dependency Injection

    Allow more than one child component of the same type. Allow child components to be placed within the ...

  2. [Angular] Component's dependency injection

    An Angular service registered on the NgModule is globally visible on the entire application. Moreove ...

  3. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

  4. AngularJs学习笔记--Dependency Injection(DI,依赖注入)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...

  5. 清晰架构(Clean Architecture)的Go微服务: 依赖注入(Dependency Injection)

    在清晰架构(Clean Architecture)中,应用程序的每一层(用例,数据服务和域模型)仅依赖于其他层的接口而不是具体类型. 在运行时,程序容器¹负责创建具体类型并将它们注入到每个函数中,它使 ...

  6. .NET编程5月小结 - Blazor, Unity, Dependency Injection

    本文是我在5月份看到的一些有趣的内容的集合.在这里你可以找到许多有关Blazor.ASPNET Core的学习资源和示例项目,有关在Unity中使用Zenject进行单元测试的博客,有关Unity项目 ...

  7. Ninject学习(一) - Dependency Injection By Hand

    大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...

  8. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  9. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

随机推荐

  1. 《DSP using MATLAB》Problem 2.2

    1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...

  2. 【传输协议】发送https请求,由于客户端jdk版本过高,服务端版本低。导致异常:javax.net.ssl.SSLHandshakeException: Server chose SSLv3, but that protocol version is not enabled or not supported by the client.

    本地环境jdk为1.8,服务器使用jdk版本未知.但发送https请求,抛出如下异常,解决方案. 一:发送异常内容如下 javax.net.ssl.SSLHandshakeException: Ser ...

  3. Linux驱动调试学习笔记

    1. struct task_struct current->comm[16]; /*此进程可执行文件的名字!!可只对此进程执行打印*/

  4. USB学习笔记-总结

    1. # ls /sys/bus/usb/devices/解析:1-0:1.0 1-1 1-1:1.0 2-0:1.0 2-1 2-1:1.0 2-2 2-2.1 2-2:1.0 2-2.1:1.0 ...

  5. 【转】在Visual Studio中怎样快速添加代码段

    原文网址:http://blog.csdn.net/yl2isoft/article/details/9735527 以前一直只知道,键入prop,再按两次tab键,会生成自动属性代码. 今天闲着无事 ...

  6. windows server 2012 AD 域和站点部署系列

    http://blog.csdn.net/ronsarah/article/category/1495599 http://blog.csdn.net/david_520042/article/cat ...

  7. thinkphp配置rewrite模式访问时不生效 出现No input file specified解决方法

    使用thinkphp配置rewire模式的路径访问网站时, 直接复制官网的.htaccess文件的代码复制过去 1 2 3 4 5 6 <IfModule mod_rewrite.c>   ...

  8. vmware克隆linux网络配置

    一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...

  9. 虚拟机Linux桥接模式下设置静态IP

    之前一直使用NAT模式,测试时android端远程访问虚拟机的mysql时发现无法连接,但是访问同学拷过来的虚拟机Linux的mysql却成功了,想了下原因是他设置的桥接模式.关于两种模式的区别,网上 ...

  10. CentOS iptables防火墙的基本应用讲解

    iptables是Linux下不错的防火墙软件,本文主要给大家介绍下iptables的安装.规则增加和清除.开放指定端口.屏蔽指定ip和ip段等CentOS下iptables的基本应用. 一.ipta ...