[Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are preserved when transpiled to ES5, TypeScript emits metadata. In this lesson we’ll explore how the @Injectable decorator ensures metadata generation for services that have their own dependencies.
For exmaple, the TodoService looks like this:
export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
];
getTodos(){
return this.todos;
}
}
Now we want to inject LoggerProvider into TodoService:
import {LoggerProvider} from './LoggerProvider';
export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
];
constructor(private logger: LoggerProvider){
}
getTodos(){
this.logger.debug('Items', this.todos);
return this.todos;
}
}
If we want to the code, it will show errors:
Cannot resolve paramster in TodoService
The problem for this is because, when TypeScript code compile to ES5 code, 'LoggerProivder' is injected into the TodoService to decreator (Angular creates it).
We must provider decreator to the TodoService in order to let this class know how to handle DI. So to solve the problem, we just need to add '@Injectable' whcih provided by Angular:
import {LoggerProvider} from './LoggerProvider';
import {Injectable} from '@angular/core';
@Injectable
export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
];
constructor(private logger: LoggerProvider){
}
getTodos(){
this.logger.debug('Items', this.todos);
return this.todos;
}
}
[Angular 2] Understanding @Injectable的更多相关文章
- [Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...
- [Angular 2] Understanding Pure & Impure pipe
First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...
- 来自 Thoughtram 的 Angular 2 系列资料
Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...
- [Angular 2] Value Providers & @Inject
Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...
- [Angular 2] Handle Reactive Async opreations in Service
When you use ngrx/store and you want to fire a service request. When it sucessfully return the respo ...
- 使用angular的HttpClient搭配rxjs
一.原Http使用总结 使用方法 在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } fr ...
- Angular路由——路由守卫
一.路由守卫 当用户满足一定条件才被允许进入或者离开一个路由. 路由守卫场景: 只有当用户登录并拥有某些权限的时候才能进入某些路由. 一个由多个表单组成的向导,例如注册流程,用户只有在当前路由的组件中 ...
- 【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例
目录 一. 划重点 二. Angular应用中的Http请求 三. 使用Rxjs构建Http请求结果的处理管道 3.1 基本示例 3.2 常见的操作符 四. 冷热Observable的两种典型场景 4 ...
- angular default project (angular.json的解读)
Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...
随机推荐
- 【转】linux线程模型
一.定义 关于进程.轻量级进程.线程.用户线程.内核线程的定义,这个很容易找到,但是看完之后你可以说你懂了,但实际上你真的明白了么? 在现代操作系统中,进程支持多线程.进程是资源管理的最小单元:而线程 ...
- How to easily create popup menu for DevExpress treelist z
http://www.itjungles.com/how-to-easily-create-popup-menu-for-devexpress-treelist.html Adding popup m ...
- (转载) 数组a[]={3,5,2,4,1,8},要求从a中找出所有“和”等于10的子集
背包问题. 不过就这道题目本身而言,由于集合a中只要6个元素,而不是成千上万,所以可以使用更直观的办法: 只要你能通过程序给出数组a中元素所组成的集合的所有的子集合(幂集),那么只需在 ...
- codeforces 691F Couple Cover 暴力
分析:开一个300w的数组,统计,然后nlogn统计每个值在在序对第一个出现有多少种情况 时间复杂度:O(nlogn) n在3e6数量级 #include<cstdio> #include ...
- HDU5697 刷题计划 dp+最小乘积生成树
分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...
- 【C#】字符串与字符数组
字符串与字符数组的相互转换. 字符串转换成字符数组: string ss="abcdefg"; char[] cc=ss.ToCharArray(); 字符数组转换成字符串 ...
- 微软控制台带来的PHP控制台输出问题
/** * 测试文件包含方式对跨平台的影响 * 控制台下测试. * 默认的文件编码为 UTF-8 */ function testChinese() { $file = __DIR__ . '/con ...
- 基于Python的Grib数据可视化
http://www.cnblogs.com/kallan/p/5160017.html
- HttpComponents 学习的两个重要文档
httpcore-tutorial-simplified-chinese httpclient-tutorial-simplified-chinese
- sonar 代码质量管理平台
1) 下载 从sonar官网http://www.sonarsource.org/下载 (版本当然是最新的了) 在官网上是不分系统的,一个zip包,下下来之后,包里包含 windows .l ...