angular服务二
angular服务
###$http
***
- 实现客户端与服务器端异步请求
get方式
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtr">
{{data}}
</div>
<script src="angular.min.js"></script>
<script src="jquery-3.1.1.min.js"></script>
<script>
var app = angular.module("myApp", [])
app.controller('myCtr', ["$scope","$http", function($scope,$http){
$http({ //$http配置快
method:'get',
url:'1.php',
cache:true //设置为true后,会将异步请求数据保存到缓存中,下次请求便不会请求服务器了,从缓存中取数据即可,减少服务器压力
}).then(function(response){ //then方法共有两个参数,第一个返回成功信息,第二个返回失败信息(与服务器异步请求有成功、失败两种情况)
$scope.data = response.data; //将返回信息的data字段作为数据绑定
},
function(response){ //第二个返回失败信息
})
}]);
</script>
</body>
</html>
1.php
<?php
$data = ["name"=>"刘德华", "title"=>"长城"];
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
post方式
- 去Network下的XHR下查看结果
- 方式一
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtr">
{{data}}
</div>
<script src="angular.min.js"></script>
<script src="jquery-3.1.1.min.js"></script>
<script>
var app = angular.module("myApp", [])
app.controller('myCtr', ["$scope","$http", function($scope,$http){
$http({
method:"post",
url:"1.php",
data: $.param({id:'007',name:'奔波霸'}),
headers:{"Content-type":"application/x-www-form-urlencoded"}
}).then(function(reponse){
},
function(response){
});
}]);
</script>
</body>
</html>
1.php
<?php
print_r($_POST);
?>
- 方式二
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtr">
{{data}}
</div>
<script src="angular.min.js"></script>
<script src="jquery-3.1.1.min.js"></script>
<script>
var app = angular.module("myApp", [])
app.controller('myCtr', ["$scope","$http", function($scope,$http){
$http({
method:"post",
url:"1.php",
data: {id:'007',name:'奔波霸'}
}).then(function(reponse){
},
function(response){
});
}]);
</script>
</body>
</html>
1.php
<?php
$content = file_get_contents('php://input');
print_r(json_decode($content, true));
?>
get请求简写
$http.get("1.php",{cache:true}).then(function(response){
},
function(response){
});
自定义服务(factory)
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtr">
{{data}}
</div>
<script src="angular.min.js"></script>
<script src="jquery-3.1.1.min.js"></script>
<script>
var app = angular.module("myApp", []);
app.factory("vedioServer",["$http",function($http){
return {
vedioS: function(){
return $http({url:'1.php'});
}
}
}]);
app.controller('myCtr', ["$scope","vedioServer", function($scope,vedioServer){
vedioServer.vedioS().then(function(response){
$scope.data = response.data;
})
}]);
</script>
</body>
</html>
1.php
<?php
$data = ["name"=>"刘德华", "title"=>"长城"];
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
自定义服务(service)
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body ng-app="myApp">
<div ng-controller="myCtr">
{{data}}
</div>
<script src="angular.min.js"></script>
<script src="jquery-3.1.1.min.js"></script>
<script>
var app = angular.module("myApp", []);
app.service("vedioServer",["$http", function($http){
this.vedioS = function(response){
return $http({url:'1.php'}).then(function(response){
return response.data;
});
}
}])
app.controller('myCtr', ["$scope","vedioServer", function($scope,vedioServer){
vedioServer.vedioS().then(function(data){
$scope.data = data;
})
}]);
</script>
</body>
</html>
1.php
<?php
$data = ["name"=>"刘德华", "title"=>"长城"];
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
angular服务二的更多相关文章
- angular服务一
angular服务 [$apply()] jquery内数据绑定 要用$apply(),不然不能在js内通过angular绑定数据 <!DOCTYPE html> <html lan ...
- Springboot & Mybatis 构建restful 服务二
Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...
- 5、SAMBA服务二:配置实例
①:SAMBA服务一:参数详解 ②:SAMBA服务二:配置实例 5.2.3.Samba共享目录配置实例 1.允许匿名用户读取/it共享目录,修改/etc/samba/smb.conf,在最后添加以下内 ...
- angular 服务 service factory provider constant value
angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): ...
- openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 二
openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...
- .NET Core微服务二:Ocelot API网关
.NET Core微服务一:Consul服务中心 .NET Core微服务二:Ocelot API网关 .NET Core微服务三:polly熔断与降级 本文的项目代码,在文章结尾处可以下载. 本文使 ...
- 腾讯云OCR服务二次开发
本文记录了对腾讯云OCR服务二次开发的代码和开发过程中遇到的问题.
- 理解 Angular 服务
理解 Angular 服务 本文写于 2021 年 3 月 29 日 理解 Angular 服务 什么是服务 服务写法 原理简述 提供服务 1. 在服务中注册 2. 在 module 中注册 3. 在 ...
- angularJS 服务二
$http服务 一 介绍 AngularJS为我们提供了很多种服务,$http用于发送http请求,动态的请求数据.我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏 ...
随机推荐
- git 管理
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #3e3e3e; bac ...
- centos 域名硬解析(linux)
centos做硬解析跟Windows一样修改一个文件. 具体文件为:/etc/hosts.修改命令: vi /etc/hosts 格式个Windows也一样的.
- IOS 杂笔-11(实现在外部无法改变UIView的size)
我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容. 在外部无法改变UIVIew控件的size. 这里说是UIView,但是事实上,是大多数控件而绝非仅UIView. 想要实现在外部无法改 ...
- SqlServer表结构查询
一.前言 近两天项目升级数据迁移,将老版本(sqlserver)的数据迁移到新版本(mysql)数据库,需要整理一个Excel表格出来,映射两个库之间的表格字段,示例如下: Mysql数据库查询表结构 ...
- 基于IIS构建Pyathon Web服务
本文简单叙述了在Windows下,如何利用IIS构建Python Web服务. 其主要步骤如下: 1.在IIS下构建一个站点,如图: 2.配置Python文件的处理程序,如图: 3.最后,在对应站点根 ...
- Mysql查询语句使用select.. for update导致的数据库死锁分析
近期有一个业务需求,多台机器需要同时从Mysql一个表里查询数据并做后续业务逻辑,为了防止多台机器同时拿到一样的数据,每台机器需要在获取时锁住获取数据的数据段,保证多台机器不拿到相同的数据. 我们My ...
- Linux程序包管理之yum及源代码安装
第十六章.Linux程序包管理之yum及源代码安装 目录 yum介绍 yum配置文件 yum的repo配置文件中可用的变量 yum命令的使用 使用光盘作为本地yum仓库 如何创建yum仓库 编译安装的 ...
- linux基本知识2
date:时间管理 linux时钟: 硬件时钟:hwclock -s:硬件时钟到系统时钟 -w:系统时钟到硬件时钟 系统时钟:date 如何查看是外部命令还是内部命令: type COMMAND ...
- spring bean的生命周期
掌握好spring bean的生命周期,对spring的扩展大有帮助. spring bean的生命周期(推荐看) spring bean的生命周期
- Nodejs安装
1 下载NodeJS https://nodejs.org/download/ 最新版下载地址 # wget https://nodejs.org/dist/v0.12.7/node-v0.12. ...