Orgial aritial --> Link

The problem with Angular 1 DI:

Angular 2 DI:

  • Solve the singletons problem:

The service you inject to the parent component can be differnet with the one you inject to child component:

var injector = ReflectiveInjector.resolveAndCreate([Engine]);
var childInjector = injector.resolveAndCreateChild([Engine]); injector.get(Engine) !== childInjector.get(Engine);

`resolveAndCreate` & `resolveAndCreateChild` are function to create injector.

Even here use the same service `Engine`, but the instances are different.

In Angular2, it looks like:

// child component
@Component({
selector: 'child',
providers: [Engine],
template: '<h1> childcomponent !</h1>'
})
class Child{
...
} // parnet component
@Component({
selector: 'parent',
providers: [Engine],
template: '<h1> parent component !</h1>'
})
class Parent {
...
}

The `Engine` we inject into Child component is a new instance, which is not the same as parent one.

So what if we want to share the same instance?

Well, If child component and parent component want the same service, then we only inject servie to parent component. The child component can access parent component's injected service.

So in code, it will looks like:

// child component
@Component({
selector: 'child',
providers: [],
template: '<h1> childcomponent !</h1>'
})
class Child{
...
} // parnet component
@Component({
selector: 'parent',
providers: [Engine],
template: '<h1> parent component !</h1>'
})
class Parent {
...
}

We just remove 'Engine' from Child component, now they share the same service instance.

  • Solve name collision problem:

Angular 2 allows you configure the service differently:

  1. useClass:
provide(Engine, {useClass: OtherEngine})

  2. useValue:

provide(String, {useValue: 'Hello World'})

  3. useExisting:

provide(V8, {useExisting: Engine})

  4. useFactory:

provide(Engine, {useFactory: () => {
return function () {
if (IS_V8) {
return new V8Engine();
} else {
return new V6Engine();
}
}
}})

Of course, a factory might have its own dependencies. Passing dependencies to factories is as easy as adding a list of tokens to the factory:

provide(Engine, {
useFactory: (car, engine) => { },
deps: [Car, Engine]
})

[Angular 2] DI in Angular 2 - 1的更多相关文章

  1. 原创:Javascript DI!Angular依赖注入的实现原理

    DI是Angular的特色功能,而在Angular 2.0的计划中,DI将成为一个独立的模块,参见 https://github.com/angular/di.js 这意味着它也有机会被用于nodej ...

  2. Angular学习笔记:Angular CLI

    定义 Angular CLI:The Angular CLI is a command line interface tool that can create a project, add files ...

  3. angular enter事件,angular回车事件

    angular回车键搜索,angular enter搜索 对于搜索框,用户在输入内容后的搜索习惯不是鼠标点击搜索按钮,而是直接按enter键,那我们怎么给enter键绑定事件呢,其实很简单,代码如下: ...

  4. Angular 2 升级到 Angular 5

    Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...

  5. Angular系列一:Angular程序架构

    Angular程序架构 Angular程序架构 组件:一段带有业务逻辑和数据的Html服务:用来封装可重用的业务逻辑指令:允许你向Html元素添加自定义行为模块: 环境搭建 安装nodeJs安装好no ...

  6. [Angular] Advanced DI

    In this post, we are going to see how to solve one design pattern challenge. The challenge is what w ...

  7. 30行代码让你理解angular依赖注入:angular 依赖注入原理

    依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...

  8. Angular企业级开发(3)-Angular MVC实现

    1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...

  9. angular源码分析:angular中脏活累活承担者之$parse

    我们在上一期中讲 $rootscope时,看到$rootscope是依赖$prase,其实不止是$rootscope,翻看angular的源码随便翻翻就可以发现很多地方是依赖于$parse的.而$pa ...

随机推荐

  1. SAR ADC简介

    SAR型 (逐次逼近型) 摘要:逐次逼近寄存器型(SAR)模数转换器(ADC)占据着大部分的中等至高分辨率ADC市场.SAR ADC的采样速率最高可达5Msps,分辨率为8位至18位.SAR架构允许高 ...

  2. JS实现精确加减乘除

    说明:项目中要使用 JS 实现自动计算的功能,进行一些浮点数运算时,计算结果却是一长串的值,这里提供一个解决方法,问题基本上可以解决. 具体代码如下: //加法函数 function accAdd(a ...

  3. VMware下打开Chrome OS遇到没有网络连接可用

    打开ChromeOS.vmx文件,最后一行添加 ethernet0.virtualDev = "e1000" 就能解决.

  4. Js处理json数据

    js中处理由ajax调用返回的json数据问题,可以通过使用JSON.parse方法将json字符串转化成javascript 对象.通过对象访问属性值. JSON.parse 只限于高版本的浏览器. ...

  5. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)

    BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...

  6. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...

  7. [OJ] Matrix Zigzag Traversal

    LintCode #46. Matrix Zigzag Traversal (Easy) class Solution { public: vector<int> printZMatrix ...

  8. Drainage Ditches(Dinic最大流)

    http://poj.org/problem?id=1273 用Dinic求最大流的模板题,注意会有重边. 邻接矩阵建图 #include<stdio.h> #include<str ...

  9. Continue 的应用(暂时还不大会运用)

    static void Main(string[] args)        {            while (true)            {                        ...

  10. 如何修复在Microsoft Azure中“虚拟机防火墙打开,关闭RDP的连接端口”问题

     注:下列步骤并不一定适用所有场景,提供思路,请灵活应用 我们在使用Microsoft Azure 中Windows 虚拟机,有时会发生错误打开防火墙或一些管家软件错误的关闭了"远程桌面 ...