惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 风轻如梦

阿里云专属推荐码nuyxa6 WHY JAVASCRIPT NEEDS TYPES BUILDING ANGULAR APPS USING FLUX ARCHITECTURE TWO PHASES OF ANGULAR 2 APPLICATIONS Forms in Angular 2 CHANGE DETECTION IN ANGULAR 2 BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2 ddd http://angular.github.io/router/ background image http://4526621.blog.51cto.com/4516621/1343369 http://www.atool.org/keytype.php#0-tsina-1-53371-397232819ff9a47a7b7e80a40613cfe1 yahoo 待做 qiangchezu car contact qq 30288891@qq.com http://depositphotos.com/ together 矢量图 耳石症
ANGULAR 2 BITS: UNIFIED DEPENDENCY INJECTION
风轻如梦 · 2015-04-08 · via 博客园 - 风轻如梦

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) {
        

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.

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

    •  
    • Reply
  • Avatar

    Robert Penner • a month ago

    Looks like a typo: Aucotomplete

  •  
  • Reply
    • Avatar

  • Avatar

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

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

    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