Ⅳ.AngularJS的点点滴滴-- 服务
服务(Angularjs很多方法都是服务组成的)
1.使用service方法创建的单例服务
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<div ng-controller="detail">
<input type="number" ng-model="x">
<input type="number" ng-model="y">
<button ng-click="add()">总和</button>
<br />
<label>{{z}}</label>
</div>
<script>
angular.module('app.service', [])
.config(['$provide',function($provide){
$provide.service('calc',[function(){
this.plusnum="";
this.add = function(x,y) {
this.plusnum+="+";
return x+y+this.plusnum;
}
}]);
}]);
angular.module('app', ['app.service'])
.controller('detail',['$scope','calc',function($scope,calc) {
angular.extend($scope,{
x:0,y:0,z:0
});
$scope.add=function(){
$scope.z=calc.add($scope.x,$scope.y);
}
}]);
angular.bootstrap(document, ['app']);
</script>
</html>
$scope加属性的时候切莫使用scope={x:0},会覆盖掉原来的对象,让功能失效,
因为是一个实例,所以每次调用服务plusnum都会存在上一次的
2.使用factory方法创建服务
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<div ng-controller="detail">
<input type="number" ng-model="x">
<input type="number" ng-model="y">
<button ng-click="add()">总和</button>
<br />
<label>{{z}}</label>
</div>
<script>
angular.module('app.service', [])
.config(['$provide',function($provide){
$provide.factory('calc', [function(){
return {add :function(x,y) {
return x+y;
}
}
}]);
}]);
angular.module('app', ['app.service'])
.controller('detail',['$scope','calc',function($scope,calc) {
angular.extend($scope,{
x:0,y:0,z:0
});
$scope.add=function(){
$scope.z=calc.add($scope.x,$scope.y);
}
}]);
angular.bootstrap(document, ['app']);
</script>
</html>
用factory方法创建一个服务,需要返回值,否则就是undefined,
如果是方法使用的时候需要实例化或者直接返回一个对象,上面的例子是直接返回对象
3.使用服务的时候也许会用到$http请求或者使用资源ngResource,资源具体下一个点滴,$http和ajax类似使用简单不过前题是要注入参数
angular.module('app.service', [])
.config(['$provide',function($provide){
$provide.service('test',['$http',function($http){
this.test = function() {
var config={
method:'jsonp',
url:'http://geoip.weather.com.cn/g/',
headers :{},
data :{a:'test'},
cache :false,
transformRequest:function(data, headersGetter),
transformResponse:function(data, headersGetter),
xsrfHeaderName:'',
xsrfCookieName :'',
withCredentials:true,
timeout :'1000',
responseType :'json',
};
$http(config);
.success(function(){}))
.error(function(){})
}
}]);
}]);
基本配置和ajax类似,也可以直接使用$http.get(url,config)这些来调用,其中主要区别是getJSON和jsonp的方法名称,以及几个不常用的方法$http.head$http.post$http.put$http.delete
- 其中xsrfHeaderName和xsrfCookieName、withCredentials主要用来跨域的时候验证,不在angularjs范围内
具体内容可以参考HTTP access control- 其中transformRequest和transformResponse的参数是一个方法或者一个数组的方法
- 下一篇:Ⅴ.AngularJS的点点滴滴-- 资源和过滤
- 上一篇:Ⅲ.AngularJS的点点滴滴-- 路由
- 本文链接地址:Ⅳ.AngularJS的点点滴滴-- 服务
Ⅳ.AngularJS的点点滴滴-- 服务的更多相关文章
- Ⅴ.AngularJS的点点滴滴-- 资源和过滤
资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...
- Ⅲ.AngularJS的点点滴滴-- 路由
路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...
- AngularJs之六(服务)
服务:AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用.AngularJS 内建了30 多个服务. 最常用的服务:$location 服务, $http 服务 ...
- 让AngularJS的$http 服务像jQuery.ajax()一样工作
让AngularJS的$http 服务像jQuery.ajax()一样工作 $http的post . 请求默认的content-Type=application/json . 提交的是json对象的字 ...
- Ⅶ.AngularJS的点点滴滴-- 事件
事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...
- Ⅵ.AngularJS的点点滴滴-- 指令
指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...
- Ⅱ.AngularJS的点点滴滴--缓存
模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...
- Ⅰ.AngularJS的点点滴滴--引导
AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 页面引导实例化 1.自动实例化 <html> <script ...
- 怎么理解angularjs中的服务?
AngularJS中的服务其实就是提供一种方式抽取共用类库 比如说一些工具类方法,我们传统的做法就是自己写个 utility 类,把相关的工具方法填充到utility里面去,最后把utility类放到 ...
随机推荐
- Linq 筛选出一条数据
InBoxInfo boxInfo = boxList.Find(p => p.GoodsID == goods.GoodsID.ToString().Trim() && p.S ...
- OA学习笔记-009-岗位管理的CRUD
一.分析 Action->Service->Dao CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承 ...
- Delphi XML-RPC 中文乱码解决方法
http://download.csdn.net/user/csm2432/uploads/2
- 带圆角的EditText
转载请注明出处:http://blog.csdn.net/krislight/article 1.定义一个Drawable <?xml version="1.0" encod ...
- 监控持有sql和被堵塞的sql
Session 1: mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update Client ...
- Python 几个重要的内置函数
所谓内置函数,就是在Python中被自动加载的函数,任何时候都可以用.内置函数,这意味着我们不必为了使用该函数而导入模块.不必做任何操作,Python 就可识别内置函数.在学习Python的过程中,有 ...
- vijosP1903学姐的实习工资
描述 学姐去实习了, 一共实习了N天, 每一天都可以得到实习工资V[i], 这里V[1..N]被看作是整数序列.因为学姐很厉害, 所以V[1..N]是不下降的.也就是说学姐每天的工资只会越来越多, 不 ...
- Cocoapods 64-bit(iPhone5s) 问题解决方案
把 主工程 和 Pods 中的所有的 Architectures --> Architectures 改为 Standard architectures(arvmv7, armv7s), 去掉 ...
- 【转】 Android SDK无法更新解决方法---不错
原文网址:http://blog.csdn.net/shi_weihappy/article/details/41847997 自己的修改: 203.208.39.238 dl.google.com7 ...
- SharePoint 2010 安装简介及相关补丁下载
转:http://www.cnblogs.com/jianyus/archive/2011/10/28/2228212.html 1.安装Windows Server 2008 系统,这个我就不说了, ...