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 ›
    •  
       

最新文章

  1. Code First数据库迁移
  2. jq插件制作(力推)
  3. 两种让tableview返回顶部的方法
  4. git初体验(五)SSH的理解
  5. Android Manifest 权限描述大全
  6. cocos2d-x 3.0rc2中读取sqlite文件
  7. java 21-13 合并
  8. 蓝灯官网下载,蓝灯最新版下载,Lantern(蓝灯)
  9. 【jQuery】使用JQ来编写面板的淡入淡出效果
  10. npoi导入--从varchar数据类型到datetime数据类型转换产生一个超出范围的值问题
  11. Visual Studio 20周年,我和VS不得不说的故事(内含福利)
  12. django报错Manager isn&#39;t accessible via UserInfo instances
  13. delphi 中实现当期日期 减去 若干小时的方法
  14. 数据库between and
  15. Centos 6\7下yum安装rstudio-server\shiny-server
  16. phpstorm 不能自动打开上次的历史文件
  17. 分布式缓存技术redis系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
  18. Ubuntu16.04下安装QQ的完整操作记录(经验证可用)
  19. logstash快速入门
  20. WPF 简易进度条效果

热门文章

  1. [LOJ535]「LibreOJ Round #6」花火
  2. 几种Python执行时间的计算方法
  3. 二:状压dp
  4. bzoj 3730 震波——动态点分治+树状数组
  5. weex入门篇
  6. WinForm 每用户只允许创建一个实例
  7. Navicat Premium解决连接mssql报错的问题
  8. C# 添加xml节点多了xmlns属性问题
  9. 扩展Linq的Distinct方法动态根据条件进行筛选
  10. 教你判断一个APP页面是原生的还是H5页面 。(还没看)