[Angular 2] Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory providers. The example used in this lesson builts upon the previous lesson on understanding factory providers.
For example, the LoggerProvider need to use ConsoleService, so when you put LoggerProvider into 'providers' array, we also need to provide dependence which refer to ConsoleService, the way to do this:
providers: [
TodoService,
ConsoleService,
,{
provide: LoggerProvider, useFactory: (consoleService) => {
return new LoggerProvider(consoleService, true)
},
deps: [ConsoleService]
}
],
We add 'deps' prop. The order you write into 'deps' also affect the one you inject to useFactory() function:
providers: [
TodoService,
ConsoleService,
TranslateService,
,{
provide: LoggerProvider, useFactory: (cs, ts) => {
return new LoggerProvider(cs, ts, true)
},
deps: [ConsoleService, TranslateService]
}
],
'cs' is the instance of 'ConsoleService'; and 'ts' is the instance of 'TranslateService'.
[Angular 2] Factory Provider with dependencies的更多相关文章
- [Angular 2] Factory Provider
In this lesson, we discuss how and when to use factory providers, to enable dependencies that should ...
- angular 服务 service factory provider constant value
angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): ...
- angularjs---服务(service / factory / provider)
初angularJs时 常写一些不够优雅的代码 !我总结了一下看看各位有没有中枪的!-----( 这里只针对服务service及其相关! ) 以下做法不太优雅 兄弟controller 之间的相同 ...
- Pipeline模式与Factory+Provider模式的应用
前言 我正在写FastGithub这个小麻雀项目,里面主要涉及了Pipeline模式和Factory+Provider模式,这两种设计模式,让这个项目在"ip扫描"和"i ...
- ionic准备之angular基础———服务provider 和 factory和service(9)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- angular 关于 factory、service、provider的相关用法
1.factory() Angular里面创建service最简单的方式是使用factory()方法. factory()让我们通过返回一个包含service方法和数据的对象来定义一个service. ...
- [Angular] Tree shakable provider
When we create a Service, Angluar CLI will helps us to add: @#Injectable({ providedIn: 'root' }) It ...
- Angular 学习笔记——$provider
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- [Angular] Using useExisting provider
Unlike 'useClass', 'useExisting' doesn't create a new instance when you register your service inside ...
随机推荐
- HDU 5119 Happy Matt Friends
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...
- Android Dialog用法
摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的 ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- 国内外开源与 SaaS ,团队协作平台、项目管理工具整理
整理一些开源与 SaaS ,团队协作平台.项目管理工具.还有哪些比较好的工具,可以推荐下? 名称 地址 备注 asana https://asana.com/ 国外 basecamp https:// ...
- Struts2动态调用DMI及错误解决方法
在Strust2中action可以定义自己的方法,调用方法有两种方式,一种方式是struts.xml中指定method来表示需要用到的方法, 但是这种方法缺点在于如果你的Action中有很多方法则要多 ...
- Python绘图和数值工具:matplotlib 和 numpy下载与使用
安装任何python模块的标准方式是使用标准的python版本,然后添加标准的模块最简单的方法是登陆相应的网站下载程序包. 但是要考虑依赖关系 , 平台和Python版本号. windows一般带有安 ...
- bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...
- (转载)OC学习篇之---第一个程序HelloWorld
之前的一片文章简单的介绍了OC的相关概述,从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这 ...
- Tkinter教程之Label篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811293 #Tkinter教程之Label篇'''1.Label的第一个例子text属性使用 ...
- 给MyEclipse 10增加SVN功能
1.在myeclipse的安装目录下 myeclipse 10文件夹下的 dropins文件夹新建一个文件夹 svn. 2.然后下载SVN插件:svn插件网站:http://subclipse.tig ...