[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 中引入创建的服务 ③利用本地存储实现数据持久化 ④在组件中使用
随机推荐
- 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间
链接:http://www.woqutech.com/?p=1116 MySQL 本身通过 show slave status 提供了 Seconds_Behind_Master ,用于衡量主备之间的 ...
- Oracle 存储过程的创建,及触发器调用存储过程
一.创建存储过程 1.存储过程写法 create or replace procedure HVM_BYQ_TJ --变压器统计信息--->入库 (id in number) as begin ...
- ashx-auth-黑色简洁验证码
ylbtech-util: ashx-auth-黑色简洁验证码 ashx-auth-黑色简洁验证码 1.A,效果图返回顶部 1.B,源代码返回顶部 /ImageUniqueCode.ashx &l ...
- linux常用命令之--磁盘管理命令
linux的磁盘管理命令 1.查看磁盘空间 df:用于显示磁盘空间的使用情况 其命令格式如下: df [-option] 常用参数: -i:使用inodes显示结果 -k:使用KBytes显示结果 - ...
- <转>详解DNS的常用记录(上):DNS系列之二
详解DNS的常用记录(上) 在上篇博文中,我们介绍了DNS服务器的体系结构,从中我们了解到如果我们希望注册一个域名,那么必须经过顶级域名服务器或其下级的域名服务器为我们申请的域名进行委派,把解析权委派 ...
- nginx配置:location配置方法及实例详解
今天深入研究了下nginx的location的用法,已经一些需要注意的细节,现在做一个归纳总结,以备后面查询. location匹配的是nginx的哪个变量? $request_uri locatio ...
- JQuery笔记:JQuery和JavaScript的联系与区别
来源:http://www.ido321.com/1019.html ps:LZ觉得这个标题有点大了,超出了能力范围,不喜勿碰.目前只记录LZ能力范围内的,日后持续补充. 一.JQuery对象和DOM ...
- 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(二)
第二步:登录到虚拟机 一旦创建好虚拟机后,默认azure会打开TCP 22端口,即SSH的端口.所以,我们可以通过远程连接,访问和管理该虚拟机. 首先,下载一个PuTTY软件.该软件很简单,就一个可执 ...
- 线段和矩形相交 POJ 1410
// 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...
- JSON 省市数据包括港澳
data: [{ name: "北京", cities: ["西城", "东城", "崇文", "宣武&quo ...