angular之Http服务
原文
https://www.jianshu.com/p/53e4a4bfad7d
大纲
1、什么是angular服务
2、服务的类别
3、认识angular的Http请求
4、简单实例
5、angular的http模块
6、angular官网的英雄编辑器的服务代码
7、angular代码资源
什么是angular服务
在Angular中,我们所说的服务是指那些能够被其它的组件或者指令调用的单一的,可共享的代码块.服务能够使我们提高代码的利用率,方便组件之间共享数据和方法,方便测试和维护。
服务是一个广义范畴,包括:值、函数、或应用所需的特性
几乎任何东西都可以是一个服务,典型的服务是一个类,具有专注的、明确的用途,它应该做一件特定的事情,并把它做好。
服务无处不在。
组件类应保持精简。组件本事不从服务器获得数据,不进行验证输入,也不直接往控制台写日志。它们把这些任务委托给服务
组件的任务就是提供用户体验,仅此而已。它介于视图(由模板渲染)和应用逻辑(通常包括模型的某些概念)之间。设计良好的组件为数据绑定提供属性和方法,把其它琐事都委托给服务。
服务的类别
服务有很多类别:日志服务、数据服务、消息总线、税款计算器、应用程序配置。
认识angular的Http请求
angular的http是基于浏览器的“新特性” Fetch API 而产生的,Fetch API 和以前的 xmlhttprequest 主要功能是一样的,就是发请求.不同的地方是Fetch 是基于 promise 的,而且可以配合 service worker, stream, cache 之类的 "新特性"。
简单实例
/*
testHttp.component.ts
通过http的get请求获取本地数据
*/ import 'rxjs/add/operator/map';
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'test-http',
templateUrl: './testHttp.component.html',
})
export class TestHttpComponent implements OnInit {
public mobiles: any[];
constructor(public http: Http) {
console.log('AppComponent constructor :', 'run step constructor ');
/**
需要注意的是这里的请求文件的路径需要正确,
所以一般都会有在公有文件中定义一个baseUrl从而来定位当前的文件位置,
进而再获取文件从而获取数据。
*/
http.get('../app/page/test/testHttp.json').subscribe(res=>
this.mobiles =res.json()
);
}
ngOnInit() {
console.log('AppComponent ngOnInit :', 'run step ngOnInit ');
}
}
<!--
testHttp.component.html
-->
<h1>Angular 2 App</h1>
<ul *ngIf="mobiles">
<li *ngFor="let m of mobiles"><span>{{m.id}}</span> {{m.name}}</li>
</ul>
angular的http模块
HttpModule并不是Angular的核心模块。 它是Angular用来进行Web访问的一种可选方式,并位于一个名叫@angular/http的独立附属模块中,因此需要在app.module.ts中引入关于http的模块。
import { HttpModule, JsonpModule } from '@angular/http';
@NgModule({
imports: [
HttpModule,
JsonpModule
],
})
angular官网的英雄编辑器的服务代码
/*
hero.ts
*/
export class Hero {
id: number;
name: string;
}
/*
hero.data.ts
*/
import {Hero} from "./hero";
export const HEROS: Hero[] = [
{id: 1, name: 'Hero1'},
{id: 2, name: 'Hero2'},
{id: 3, name: 'Hero3'},
{id: 4, name: 'Hero4'},
{id: 5, name: 'Hero5'},
{id: 6, name: 'Hero6'},
{id: 7, name: 'Hero7'},
{id: 8, name: 'Hero8'}
];
/*
hero.service.ts
*/
import { Injectable } from '@angular/core';
import {Hero} from "./hero";
import {HEROS} from "./hero.data"; @Injectable()
export class HeroService {
getHeros(): Promise<Hero[]> {
return Promise.resolve(HEROS);
}
getMockHeros(): Promise<Hero[]> {
return new Promise(resolve => setTimeout(resolve(HEROS), 2000))
.then(() => this.getHeros());
}
}
/*
app.component.html
*/
import {Component, OnInit} from '@angular/core';
import {Hero} from "./User";
import {HeroService} from "./special-user.service";
/*
* 别忘记了使用@前缀
* 这里相当于组件视图
*/
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
/*
* 导出这个组件,也就是一个类
* 这里相当于组件控制器
*/
export class AppComponent implements OnInit{
heros: Hero[];
constructor(
private heroService: HeroService
){}
ngOnInit() {
this.getHeroInfo();
}
public getHeroInfo () {
this.heroService.getHeros().then(heros => {
console.log(heros);
this.heros = heros
})
}
}
/*
app.component.html
*/
<ul>
<li *ngFor="let hero of heros">{{hero.name}}</li>
</ul>
代码资源
angular实例代码中的angular-service,该项目中包含了angular服务的简单实例,也包含了angular英雄编辑器的请求服务的代码,还有我自己根据所学知识写的http服务请求数据的代码,希望能对读者有所帮助。
angular之Http服务的更多相关文章
- 介绍Angular的注入服务
其实angular的注入服务是挺复杂的,目前看源码也只看懂了一半,为了不误导大家,我也不讲敢讲太复杂,怕自己都理解错了. 首先我们要知道angular的三种注入方式: 第一种:inference va ...
- Angular.js之服务与自定义服务学习笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- angular访问后台服务及监控会话超时的封装
angular访问后台服务及监控会话超时的封装 angular本身自带访问组件http和httpclient,组件本身都是异步模式访问.本文只列举了对http组件的封装同时也一同处理会话超时监控. 获 ...
- angular中的服务
angular中的服务 angular中的服务相当于一个状态管理,可以将数据放在服务里面进行获取以及编辑. 服务的安装命令: ng g service count 安装好后,会在服务的ts文件中引入一 ...
- angular的uiRouter服务学习(4)
本篇接着上一篇angular的uiRouter服务学习(3)继续讲解uiRouter的用法 本篇主要讲解uiRouter的url路由 大多数情况下,状态是和url相关联的: 当url改变,激活对应的状 ...
- angular的uiRouter服务学习(3)
本篇接着上一篇 angular的uiRouter服务学习(2) 继续讲解uiRouter的用法 本篇主要讲解uiRouter的多个命名的视图 我们可以给ui-view元素添加ui-view的值来给它命 ...
- angular的uiRouter服务学习(2)
本篇接着上一篇 angular的uiRouter服务学习(1) 继续讲解uiRouter的用法 本篇主要讲解uiRouter的嵌套状态&嵌套视图 嵌套状态的方法: 状态和状态之间可以互相嵌套, ...
- $anchorScroll angular锚点服务
angular锚点服务 $anchorScroll 普通的html页面中,我们会通过在url后面添加#elementId的方式,将页面显示定位到某个元素上,也就是所谓的锚点. 但是在angular应用 ...
- angular的$filter服务
首先,介绍下$filter服务: 1.$filter是用来进行数据格式化的专用服务: 2.AngularJS内置了currency.date.filter.json.limitTo.lowercase ...
- -_-#【Angular】定义服务
AngularJS学习笔记 <!DOCTYPE html> <html ng-app="Demo"> <head> <meta chars ...
随机推荐
- GetInvocationList 委托链表
最近发现C#程序初始化时在构造函数中,偶尔出现事件注册不成功.后查资料发现有GetInvocationList 这么一个获取类中的委托链表的函数, 使用方法如下: 1.在需委托的类(Class1)中增 ...
- Maven学习详解(13)——Maven常用命令大全与pom文件讲解
一.Maven常用命令 1.1.Maven 参数 -D 传入属性参数 -P 使用pom中指定的配置 -e 显示maven运行出错的信息 -o 离线执行命令,即不去远程仓库更新包 -X 显示ma ...
- [置顶]
Docker学习总结(2)——Docker实战之入门以及Dockerfile(二)
csphere/php-fpm:5.4 # cd docker-training/php-fpm/ # ls Dockerfile nginx_nginx.conf supervisor_nginx. ...
- js08--函数1
函数当成类看当成匿名对象看,都是跟java吻合的,只有当成函数看(函数自己可以执行)跟java区别在这里. function fact(){} var F = fact ; 函数名是函数对象的地址,是 ...
- 提高IIS的FTP安全性 管理员的九阴真经
提高IIS的FTP安全性 管理员的九阴真经 <九阴真经>是很多武林高手蒙昧以求的武林秘籍,在系统管理员这个武林中也有很多类似<九阴真经>一样的秘籍.在这里就向大家介绍一下有关提 ...
- 【AIM Tech Round 4 (Div. 2) B】Rectangles
[链接]http://codeforces.com/contest/844/problem/B [题意] 也是道计数水题,没什么记录意义 [题解] 枚举每个点的位置在,然后往右往下 枚举和它一样颜色的 ...
- 用VBS控制鼠标,在Excel2010、2013,64位中
原作者文章地址:http://demon.tw/programming/vbs-control-mouse.html 感谢原作者的攻略.才使我学会用VBS控制鼠标. 但是问题接踵而至,Excel200 ...
- A glance on VDBI
Just like other thing in data transfter, a resource should have themselves description. And the reso ...
- shell-查看手机分辨率
使用如下命令,可以查看手机分辨率 adb shell dumpsys window displays 运行结果如下 Display: mDisplayId= init=1080x1920 480dpi ...
- 12. ZooKeeper之Java客户端API使用—创建会话。
转自:https://blog.csdn.net/en_joker/article/details/78686649 客户端可以通过创建一个ZooKeeper(org.apache.zookeeper ...