[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 ...
随机推荐
- chmod 命令 set uid ,set gid,sticky bit 说明
permission的符号模式表: 模式 名字 说明 r 读 设置为可读权限 w 写 设置为可写权限 x 执行权限 设置为可执行权限 X 特殊执行权限 只有当文件为目录文件,或者其他类型的用户有可执行 ...
- tableView被Nav挡住了
// 1. // self.navigationController.navigationBar.translucent = NO; // self.tabBarController.ta ...
- OpenTSDB案例总结
加宽行可增加扫描速度 采用组合rowkey,利用数据本地性加快扫描 少数宽行,并不比多数窄行节省空间 缩短Column family 和 column的名字 合并若干列.
- cf 219D
树形dp; 思想: 把正向边赋值为0:反向边赋值为1:然后求出点到其他点的最小距离: 两次dfs: 第一次是从下往上:记录每个点到所有子树中需要改变的边的条数: 第二次是自上往下:由父节点求子节点到所 ...
- spring初始化
* Created by litao on 15/12/29. */@Component("initTagDataProcessor")public class InitTagDa ...
- Android实战之你应该使用哪个网络库?
前言 目前基本上每个应用都会使用HTTP/HTTPS协议来作为主要的传输协议来传输数据.即使你没有直接使用HTTP协议,也会有成堆的SDK会包含这些协议,譬如分析.Crash反馈等等.当然,目前也有很 ...
- linux权限掩码
我的博客:www.while0.com 主要是在新建文件或目录的时候,控制新文件或目录的默认权限. 文件:新建文件默认没有x权限,故新建文件在umask为000时最大权限是666. 目录:新建目录默认 ...
- WordPress Events Manager插件多个跨站脚本漏洞
漏洞名称: WordPress Events Manager插件多个跨站脚本漏洞 CNNVD编号: CNNVD-201310-196 发布时间: 2013-10-15 更新时间: 2013-10-15 ...
- spoolsv.exe 报错,无法打印
在使用打印机过程中突然出现spoolsv.exe应用程序错误,内存不能written•••,检查打印驱动,打印机设置选项无法打开.怀疑是病毒所致,升级杀毒软件后安全模式下杀毒后没有发现病毒,重启后还是 ...
- [转]Unity 3D旋转矢量方向及二维平面基于一点选择另一点(Rotate a Vector3 direction & Rotate a point about another point in 2D )
http://specialwolf.blog.163.com/blog/static/124466832201301332432766/ ****************************** ...