[Angular 2] Inject Service with "Providers"
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"的更多相关文章
- [Angular 2] Inject Service
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- 在Angular中定义共享的Providers
转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...
- [Angular 2] Use Service use Typescript
When creating a service, need to inject the sercive into the bootstrap(): import {bootstrap, Compone ...
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...
- [Angular] 'providedIn' for service
There is now a new, recommended, way to register a provider, directly inside the @Injectable() decor ...
- angular js自定义service的简单示例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Angular 学习笔记——service &constant
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- Angular:使用service进行http请求的简单封装
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入HttpClientModule模块 ③在app.module.ts 中引入创建 ...
- Angular:使用service进行数据的持久化设置
①使用ng g service services/storage创建一个服务组件 ②在app.module.ts 中引入创建的服务 ③利用本地存储实现数据持久化 ④在组件中使用
随机推荐
- 【转】AVL
#include <iostream> #include <ctime> #include <queue> #include <cassert> #in ...
- Redis常用命令手册:服务器相关命令
Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...
- C# Code for Downloading Stock Symbols z
http://www.jarloo.com/download-stock-symbols/ If your using C# you can easily get the XML data using ...
- Delphi 让自己的软件实现双击打开文件 转
unit shjAssociateFileType; interface uses Windows, Registry; {将文件类型strFileExtension与程序strExeFileName ...
- CCMoveTo 等函数理解
CCMoveTo: 使用CCMoveTo action来让对象从右侧屏幕外移动到屏幕左侧.注意可以通过指定duration参数控制这一过程需要多久,这里我们随机给他2-4秒的时间. CCCallFun ...
- Lucene学习笔记:一,全文检索的基本原理
一.总论 根据http://lucene.apache.org/java/docs/index.html定义: Lucene是一个高效的,基于Java的全文检索库. 所以在了解Lucene之前要费一番 ...
- Excel动态生成JSON
在最近的一个项目中,有大量的数据源来至Excel,转成JSON供前台使用.Excel数据是人工录入的,难免会有错误,所以中间会有逻辑检查.在C#中读取Excel的方式有很多,网上一搜一大堆,这里我也贴 ...
- RestTemplate中文乱码问题
使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name); // 使用默认的解码 ...
- 利用管道实现Shell多进程
shell中有个&,表示该程序在后台执行,其实是fork了一个子进程,跟系统调用是一样的. 在实际的操作过程中,有时需要控制后台程序的个数,毕竟启动太多的后台,会对服务的性能造成影响. 所以需 ...
- codeforces 630J Divisibility
J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...