服务(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)这些来调用,其中主要区别是getJSONjsonp的方法名称,以及几个不常用的方法$http.head$http.post$http.put$http.delete

  1. 其中xsrfHeaderNamexsrfCookieNamewithCredentials主要用来跨域的时候验证,不在angularjs范围内
    具体内容可以参考HTTP access control
  2. 其中transformRequesttransformResponse的参数是一个方法或者一个数组的方法

Ⅳ.AngularJS的点点滴滴-- 服务的更多相关文章

  1. Ⅴ.AngularJS的点点滴滴-- 资源和过滤

    资源ngResource(依赖ngResource模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ ...

  2. Ⅲ.AngularJS的点点滴滴-- 路由

    路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...

  3. AngularJs之六(服务)

    服务:AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用.AngularJS 内建了30 多个服务. 最常用的服务:$location  服务,  $http 服务 ...

  4. 让AngularJS的$http 服务像jQuery.ajax()一样工作

    让AngularJS的$http 服务像jQuery.ajax()一样工作 $http的post . 请求默认的content-Type=application/json . 提交的是json对象的字 ...

  5. Ⅶ.AngularJS的点点滴滴-- 事件

    事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...

  6. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  7. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

  8. Ⅰ.AngularJS的点点滴滴--引导

    AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 页面引导实例化 1.自动实例化 <html> <script ...

  9. 怎么理解angularjs中的服务?

    AngularJS中的服务其实就是提供一种方式抽取共用类库 比如说一些工具类方法,我们传统的做法就是自己写个 utility 类,把相关的工具方法填充到utility里面去,最后把utility类放到 ...

随机推荐

  1. XSS与字符编码的那些事儿

    目录 0x00:基本介绍 0x01:html实体编码 0x02:新增的实体编码 实体编码变异以及浏览器的某些工作原理! 0x03:javascript编码 0x04:base64编码 0x05:闲扯 ...

  2. comet ajax轮询

    http://www.ibm.com/developerworks/cn/webservices/ws-tip-jaxwsrpc.html http://www.cnblogs.com/pifoo/a ...

  3. Ecmall系统自带的分页功能

    在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的详细情况.关键在于get_order_data()这个方法, ...

  4. Android4.3 蓝牙BLE初步

    一.关键概念: Generic Attribute Profile (GATT) 通过BLE连接,读写属性类小数据的Profile通用规范.现在所有的BLE应用Profile都是基于GATT的.   ...

  5. LinkedIn高级分析师王益:大数据时代的理想主义和现实主义(图灵访谈)

    转自:http://www.ituring.com.cn/article/75445 王益,LinkedIn高级分析师.他曾在腾讯担任广告算法和策略的技术总监,在此期间他发明了并行机器学习系统“孔雀” ...

  6. 如何编译Support7Demos测试appcompat

    目录(?)[-] 简介 在Eclipse中编译 使用Gradle编译 简介 Google发布的API-18中带上的support-v7包含了appcompat组件,可以在小于API-11的androi ...

  7. (转载)Linux下IPTABLES防火墙的设定

    (转载)http://www.jefflei.com/post/1760.html 1.iptables防火墙启动和停止 启动iptables防火墙时命令行输入 #service iptables s ...

  8. 动态规划(DP计数):HDU 5121 Just A Mistake

    As we all know, Matt is an outstanding contestant in ACM-ICPC. Graph problems are his favorite.Once, ...

  9. Delphi TcxTreeList 怎么设置分组

    Delphi 的TcxTreeList控件设置按种类分组,操作如下: 1. 在TcxTreeList控件中双击,打开 Bands 属性,在这里面建需要分的组,在Captions->Text 输入 ...

  10. HDU-2975 Billboard

    Billboard Time Limit : 20000/8000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...