[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 ...
 
随机推荐
- AirPlay简介
			
AirPlay是苹果公司在iOS4.2即Mac OS Mountain Lion中加入的一种播放技术.可以将iPhone.iPod.iPad和Mac上的视频镜像传送到支持AirPlay的设备上(如音响 ...
 - 一些.net开源项目
			
强大的插件系统,通过Addin构建成一个功能齐全的.net开发IDE.核心是AddInTree.跟随这个项目开发许多有用的组件,比如功能文本编辑器(ICSharpCode.TextEditor),Sh ...
 - Java Annotation手册
			
Java Annotation手册 作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig) 原文:http://www.matri ...
 - Coding.net代码托管平台建立WordPress
			
Coding.net这是一个国内新兴的代码托管平台,功能主要包括:代码托管.在线运行环境.监控代码质量,兼有一定的社交功能,在线运行环境支持Java.Ruby.Node.js.PHP.Python.G ...
 - Delphi XE5 Device compatibility
			
Delphi XE5 Device compatibility https://docs.google.com/spreadsheet/ccc?key=0AoEN2CEsVvJ0dGhVaWJE ...
 - 12.URL重写
			
为什么要URL重写?1.有利于SEO(搜索引擎优化),带参数的RUL权重较低.2.地址看起来更正规,推广uid. 如我们一般在访问网页是会带参数,http://aaa.com/view.htm?id= ...
 - git stash的使用
			
https://git-scm.com/docs/git-stash 在git svn的时候使用,提交记录的时候,有部分文件的修改不需要commit. 在向svn进行git svn dcommit的时 ...
 - 如何在多线程中调用winform窗体控件
			
由于 Windows 窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value),则可能会迫使该控件进入一种不一致的状态.还可能出现其他与线程相关的 bug,包 ...
 - Custom ASP.NET Application into SharePoint --整合ASP.NET应用程序到SharePoint
			
转:http://www.devexpertise.com/2009/02/18/integrating-a-custom-aspnet-application-into-sharepoint-par ...
 - PowerDesigner使用总结  转
			
PowerDesigner使用总结 (友情提示:本博文章欢迎转载,但请注明出处:陈新汉,http://www.blogjava.net/hankchen)一.使用PowerDesigner生成HTML ...