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. ios实现类似魔兽小地图功能 在

    写了一个类似魔兽小地图功能的控件. 比如你有一个可以放大缩小的scrollView.会在里面进行一些放大缩小,点击里面的按钮呀,等操作. 这个小地图控件.就会和你的大scrollView同步.并有缩略 ...

  2. mysql show processlist命令 详解

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  3. 微信多媒体上传图片,创建卡券上传 LOGO

    //*****************************************多媒体上传图片 begin******************************************** ...

  4. flash recovery area配置

    检查数据库是否开启闪回: SQL> select flashback_on from v$database; FLASHBACK_ON ----------------------------- ...

  5. webstrom11 激活,webstorm 2016.1激活

    http://15.idea.lanyus.com/  webstorm11注册激活,你值得拥有 webstorm 2016.1 最新激活方式:http://blog.lanyus.com/archi ...

  6. 关于自然常数e的理解

    关于自然常数\(e\)的理解 By Z.H. Fu 切问录 ( http://www.fuzihao.org ) 利息增长模型 在上中学学习对数的时候,我们就学到了一个叫做e的东西(\(e\appro ...

  7. 读取jar内的配置文件

    读取jar包内的配置文件,可以使用ResourceBundle,具体具体例子如下 import java.io.BufferedInputStream; import java.io.IOExcept ...

  8. js滑动门及对像的使用

    function scrollDoor() { } scrollDoor.prototype = { sd: function (menus, divs, openClass, closeClass) ...

  9. C#简单应用spring的例子

    接口定义 namespace SpringDemo { interface IOper { void Say(); } } 此接口的两个实现 实现1 using System; namespace S ...

  10. hdu 1443 Joseph (约瑟夫环)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...