In this lesson, we’re going to take a look at how add a class to the providers property of a component creates an actual providers. We’ll learn what a provider specifically does and how we can provide different dependencies with the same token.

import {Component} from 'angular2/core';
import {TodoService} from './../services/TodoService'; @Component({
selector: 'todos',
providers: [TodoService],
template: `
<div>
<ul>
<li *ngFor="#todo of todoService.todos
{{todo.name}}
</li>
</ul>
</div>
`
}) export class TodoList implements OnInit{ todos: Array<any> constructor(private todoService: TodoService){} ngOnInit(){
this.todos = this.todoService.getTodos();
}
}
import {Injectable} from '@angular/core';

@Injectable()
export class TodoService {
todos = [
{id: , name: "eat"},
{id: , name: "sleep"},
{id: , name: "running"},
]; getTodos(){
return this.todos;
}
}

Here we use providers, which tell Angular, we want to use TodoService, create a instance for us, and so that we can use the instance.

providers: [TodoService],

Actually, this is shorthard syntax, you can do:

providers: [
{ provider: TodoService, useClass: TodoService}
],

Here need to make sure, "provider" 's token are the same use "userClass", if you change "userClass" to some other service, it will still use the "TodoService":

providers: [
{ provide: TodoService, useClass: OtherService} // still use TodoService instead
],

To recap importing a data really just makes the type available, but doesn't give us an instance. In order to inject objects, we need providers that know how to create these objects by a given token which is a type.

[Angular 2] Inject Service with "Providers"的更多相关文章

  1. [Angular 2] Inject Service

    TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...

  2. 在Angular中定义共享的Providers

    转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...

  3. [Angular 2] Use Service use Typescript

    When creating a service, need to inject the sercive into the bootstrap(): import {bootstrap, Compone ...

  4. Guice 学习(六)使用Provider注入服务( Provider Inject Service)

    1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...

  5. [Angular] 'providedIn' for service

    There is now a new, recommended, way to register a provider, directly inside the @Injectable() decor ...

  6. angular js自定义service的简单示例

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. Angular 学习笔记——service &constant

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  8. Angular:使用service进行http请求的简单封装

    ①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建 ...

  9. Angular:使用service进行数据的持久化设置

    ①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入创建的服务 ③利用本地存储实现数据持久化 ④在组件中使用

随机推荐

  1. hdu 2155 小黑的镇魂曲(dp) 2008信息工程学院集训队——选拔赛

    感觉蛮坑的一道题. 题意很像一个叫“是男人下100层”的游戏.不过多了个时间限制,要求在限定时间内从某一点下落到地面.还多了个最大下落高度,一次最多下落这么高,要不然会摔死. 一开始想dp的,然后想了 ...

  2. <译>Selenium Python Bindings 2 - Getting Started

    Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...

  3. LeetCode题解——Regular Expression Matching

    题目: 正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现0到无穷次. 解法: 一定要注意'*'必须结合前面的字符一起使用. 代码: class S ...

  4. LeetCode题解——String to Integer(atoi)

    题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...

  5. linux 用 SSH2协议远程连接并控制 linux

    [参考链接](http://php.net/manual/zh/ssh2.installation.php) ssh2_exec 并不能打印所有的命令的提示信息 如果有返回的字符串信息,可以打印,或重 ...

  6. nservicebus教程-目录

    表的内容 开始 坚持NServiceBus 扩展 每天 举办 管理和监控 发布订阅 长时间运行的流程 定制 版本控制 常见问题解答 样品 开始 概述 NServiceBus一步一步向导 架构原则 事务 ...

  7. http协议中的Content-Type

    今天对http协议中的Content-Type有所理解了 它的主要功给我的感觉,还是在前台(客户端)给服务器传输数据时,描述这个数据的格式. 比如,我只传一个表单数据,但这个表单中只有文本,没有其它的 ...

  8. Salt自动化之自动更新Gitfs-爱折腾技术网

    Salt自动化之自动更新Gitfs-爱折腾技术网 pygit2

  9. Nginx和Tengine解决高并发和高可用,而非推荐Apache

    什么是Nginx  什么是Tengine 看看国内大公司在用Nginx和Tengine吗? 步骤一:进入 https://www.taobao.com/,按F12.可看到 有很多APP对淘宝进行请求. ...

  10. Java邮件服务学习之五:邮箱服务服务端 Apache

    Apache James(Java Apache Mail Enterprise Server)是Apache组织的子项目之一,完全采用纯Java技术开发,实现了SMTP.POP3与NNTP等多种邮件 ...