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. Maximum Product Subarray JAVA实现

    题目描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  2. Android 在安装完成界面,点击打开应用程序。在应用程序点击home键,再从桌面打开程序导致产生多个实例或者说程序被重复打开

    Android 在安装完成界面,点击打开应用程序.在应用程序点击home键,再从桌面打开程序导致产生多个实例或者说程序被重复打开. etong_123的专栏 - 博客频道 - CSDN.NET htt ...

  3. ASP.NET单点登录(代码)

    [p=25, null, left]由于某些原因,在我们的应用中会遇到一个用户只能在一个地方登录的情况,也就是我们通常所说的单点登录.在ASP.NET中实现单点登录其实很简单,下面就把主要的方法和全部 ...

  4. 一起刷LeetCode4-Median of Two Sorted Arrays

    实验室太吵了...怎么办啊... ----------------------------------------------------------------------------------- ...

  5. [Hive - Tutorial] Type System 数据类型

    数据类型Type System Hive supports primitive and complex data types, as described below. See Hive Data Ty ...

  6. 了解RFC协议号

    RFC是Request For Comment的缩写,意即“请求注解”,是由IETF管理,所有关于Internet的正式标准都以文档出版,但不是所有的RFC都是正式的标准,很多RFC的目的只是为了提供 ...

  7. Java 编程要点之并发(Concurrency)详解

    计算机用户想当然地认为他们的系统在一个时间可以做多件事.他们认为,他们可以工作在一个字处理器,而其他应用程序在下载文件,管理打印队列和音频流.即使是单一的应用程序通常也是被期望在一个时间来做多件事.例 ...

  8. Apache Spark Mesos

    Mesos是一个资源管理框架,提供类似于YARN的功能. 用户可以在其中插件式地运行Spark. MapReduce. Tez等计算框架的任务. Mesos会对资源和任务进行隔离,并实现高效的资源任务 ...

  9. work5

    这一次写的内容是黄金豆小游戏,由于现在偏重写服务器端.对于算法层面其实涉及不多,更多偏于工程上的架构. 总而言之本次作业的服务器核心是用web.py所写,而且为了方便其他用户写客户端,架构非常简单. ...

  10. CSS 去掉IE10中type=password中的眼睛图标

    在IE10中,input[type=password],如果我们输入内容后,内容会变成圆点,这与以前一样,但后面多了一个眼睛图标,我们鼠标移过去按下会出现输入内容.有时我们想去掉这功能.IE10允许我 ...