[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 ...
随机推荐
- armv7a-mediatek451_001_vfp-linux-gnueabi-gcc: directory: No such file or directory 编译error
release/vm_linux/output/hisense_android/mt5399_cn_android_JB/rel/obj/oss/source/arm_mali_ko/mali400- ...
- bzoj2427: [HAOI2010]软件安装
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- UIlabel - 富文本属性
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- ThinkPHP学习手记——环境搭建
怀着激动的心情打开了thinkPHP的文档,开启了第一次php框架学习. 下载 ThinkPHP最新版本可以在官方网站(http://thinkphp.cn/down/framework.html) ...
- 教你在你的应用程序中扩展使用dynamic类型
教你在你的应用程序中扩展使用dynamic类型 相信大家在开发中经常会接触到mvc中的ViewBag,有心的同学会,发现这就是一个dynamic类型,查看源码一谈究竟,本文也是根据dynamic来扩展 ...
- Android使用pull解析xml
一.理论准备 Pull解析器的运行方式与 SAX 解析器相似.它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件.跟SAX不同的是, ...
- BZOJ 3707: 圈地 计算几何
Description 2维平面上有n个木桩,黄学长有一次圈地的机会并得到圈到的土地,为了体现他的高风亮节,他要使他圈到的土地面积尽量小.圈地需要圈一个至少3个点的多边形,多边形的顶点就是一个木桩,圈 ...
- 今天把登陆注册给改成tab了
真是解决了一个心头大患,本来以为必须请外包公司的工程师才做,自己研究了下也给解决了. 多亏去年做原型时学习了smarty.css.html这些最基本的网站前端开发的技术.FTP上传下载.linux的基 ...
- OA学习笔记-009-岗位管理的CRUD
一.分析 Action->Service->Dao CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承 ...
- BeRoTinyPascal
https://github.com/BeRo1985/berotinypascal https://github.com/thachngopl/berotinypascal