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的更多相关文章

  1. [Angular 2] Understanding OpaqueToken

    When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...

  2. [Angular 2] Understanding Pure & Impure pipe

    First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...

  3. 来自 Thoughtram 的 Angular 2 系列资料

    Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...

  4. [Angular 2] Value Providers & @Inject

    Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...

  5. [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 ...

  6. 使用angular的HttpClient搭配rxjs

    一.原Http使用总结 使用方法 在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } fr ...

  7. Angular路由——路由守卫

    一.路由守卫 当用户满足一定条件才被允许进入或者离开一个路由. 路由守卫场景: 只有当用户登录并拥有某些权限的时候才能进入某些路由. 一个由多个表单组成的向导,例如注册流程,用户只有在当前路由的组件中 ...

  8. 【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例

    目录 一. 划重点 二. Angular应用中的Http请求 三. 使用Rxjs构建Http请求结果的处理管道 3.1 基本示例 3.2 常见的操作符 四. 冷热Observable的两种典型场景 4 ...

  9. angular default project (angular.json的解读)

    Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...

随机推荐

  1. 【转】traits技术及模板偏特化

    #include <iostream> using namespace std; struct __xtrue_type { }; // define two mark-type stru ...

  2. 在PC上收发短信--Pidgin短信(Linux Pidgin插件)

    Pidgin短信是Pidgin/empathy的一个插件.当手机收到短信进,电脑上面有提示:可以在电脑上发手机短信.让你时刻保持在电脑屏幕上,也不会错过短信.目前只支持安卓,wifi连接方式. 功能 ...

  3. hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14

    题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...

  4. android改变字体的颜色的三种方法

    写文字在Android模拟器中的方法 法一: main.xml配置文件: <TextView android:id="@+id/tv" android:layout_widt ...

  5. QC开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改

    今天在QC9.0项目中增加了几个项目,然后我的想法是:开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改 虽说qc我还是比较熟悉的,但是对于这个问题,感觉可能要用到脚本,对于脚本我一窍 ...

  6. 根据关键词获取进程ID然后杀掉进程

    例如需要杀掉监听进程,如下: [oracle@kel ~]$ ps -ef|grep lsnr oracle 4973 1 1 19:40 ? 00:00:00 /home/oracle/produc ...

  7. POJ 1004 解题报告

    1.题目描述: http://poj.org/problem?id=1004 2.解题过程 这个题目咋一看很简单,虽然最终要解出来的确也不难,但是还是稍微有些小把戏在里面,其中最大的把戏就是float ...

  8. 【bz2594】水管局长数据加强版

    题意: 给出一张n节点.m条代权无向边的无向联通图 和q个任务 1:询问一条x到y的路径 并使路径上最大权值最小 要求输出路径上最大权值 2:宣布x到y的路径报废题目保证该图永远联通 题解: 这是道凶 ...

  9. Adress

    流水账的写法:因为不想让亲爱的你只看开头就关掉了我辛苦的劳作.流水账的好处是:便于逻辑的理解 http://software.intel.com/zh-cn/blogs/2014/01/20/cent ...

  10. 执行原始的 SQL 查询

    The Entity Framework Code First API includes methods that enable you to pass SQL commands directly t ...