[Angular 2] DI in Angular 2 - 1
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:
- 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的更多相关文章
- 原创:Javascript DI!Angular依赖注入的实现原理
DI是Angular的特色功能,而在Angular 2.0的计划中,DI将成为一个独立的模块,参见 https://github.com/angular/di.js 这意味着它也有机会被用于nodej ...
- Angular学习笔记:Angular CLI
定义 Angular CLI:The Angular CLI is a command line interface tool that can create a project, add files ...
- angular enter事件,angular回车事件
angular回车键搜索,angular enter搜索 对于搜索框,用户在输入内容后的搜索习惯不是鼠标点击搜索按钮,而是直接按enter键,那我们怎么给enter键绑定事件呢,其实很简单,代码如下: ...
- Angular 2 升级到 Angular 5
Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...
- Angular系列一:Angular程序架构
Angular程序架构 Angular程序架构 组件:一段带有业务逻辑和数据的Html服务:用来封装可重用的业务逻辑指令:允许你向Html元素添加自定义行为模块: 环境搭建 安装nodeJs安装好no ...
- [Angular] Advanced DI
In this post, we are going to see how to solve one design pattern challenge. The challenge is what w ...
- 30行代码让你理解angular依赖注入:angular 依赖注入原理
依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...
- Angular企业级开发(3)-Angular MVC实现
1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...
- angular源码分析:angular中脏活累活承担者之$parse
我们在上一期中讲 $rootscope时,看到$rootscope是依赖$prase,其实不止是$rootscope,翻看angular的源码随便翻翻就可以发现很多地方是依赖于$parse的.而$pa ...
随机推荐
- The CircuitCalculator.com Blog a blog with live web calculators Home About Policies Contact PCB
PCB Trace Width Calculator 转载自:CircuitCalculator.com 关键词: PCB,Layout,电流,导线宽度. This Javascript web ca ...
- python学习之---函数进阶
一,递归函数: 做程序应该都知道,在一个函数的内部还可以调用其它函数,这叫函数的调用,但是有一种特殊的情况,在一个函数内部对自身函数的调用,我们成这为函数的递归调用. 在此,使用一个家喻户晓的例子来演 ...
- IE连EXCHANGE的HTTPS时,出现错误
"There is a problem with this website's security certificate" 这种问题,一般是SSL或CA认证证书的问题. 一般从服务 ...
- 在使用Ibatis查询数据返回时,报如下错误:java ibatis The error happened while setting a property on the result object
问题: 在使用Ibatis查询数据返回时,报如下错误: [com.show.add.proxy.SqlMapClientTemplateProxy]com.ibatis.common.jdbc.exc ...
- [topcoder]IncreasingSubsequences
http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- USACO3.34Home on the Range(DP)
之前做过一道类似的 国际象棋盘神马的.. 统计出以每个1作为右下角的最大正方形 那么以大于二到这个最大值之间为边的正方形都可以以这个为右下角 累加就可以了 dp[i][j] = min(dp[i-1] ...
- 【转】基于Ubuntu 14.04 LTS编译Android4.4.2源代码
原文网址:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码 ...
- poj 2586 Y2K Accounting Bug
http://poj.org/problem?id=2586 大意是一个公司在12个月中,或固定盈余s,或固定亏损d. 但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为 ...
- Linux下Chrome浏览器的BUG
“我胡汉三又回来了”,好久没出现在博客园了,准备考试什么的最烦躁了,今天又重新整了下我的Ubuntu,结果发现了一个Chrome浏览器的Bug,但是与其说它是个Bug,还不如说它是个Joke. 好吧, ...