服务(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. Android开发将List转化为JsonArray和JsonObject

    客户端需要将List<Object>转化为JsonArray和JsonObject的方法: 首先,List中的Object的属性需要是public: class Person { publ ...

  2. 17.1 Replication Configuration 复制配置

    17.1 Replication Configuration 复制配置 17.1.1 How to Set Up Replication 17.1.2 Replication Formats 17.1 ...

  3. 尚未在 Web 服务器上注册 ASP.NET 4.0” 的解决办法

    http://www.sowsoy.com/topics-537.html win7,vs2010创建.NetFramework 4框架下的Asp.Net空网站.系统提示 “尚未在 Web 服务器上注 ...

  4. 【Node】package.json

    npm的package.json中文文档https://github.com/ericdum/mujiang.info/issues/6

  5. sql server 清除日志

    SQL2008 的收缩日志  由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消: (SQL2005) Backup Log DNNam ...

  6. sharepoint 2010 如何创建文档库内容类型content type

    转:http://biancheng.dnbcw.info/linux/441643.html 这次主要是记录下,如何来创建文档内容类型,例如新建文档的时候,可以选择不同模板,有word,excel文 ...

  7. js前台与后台数据交互-前台调后台

    转自:http://blog.csdn.net/wang379275614/article/details/17033981   网站是围绕数据库来编程的,以数据库中的数据为中心,通过后台来操作这些数 ...

  8. Flash加载网页内容

    import flash.net.URLLoader; var m_loader:URLLoader = new URLLoader(); m_loader.addEventListener(Even ...

  9. 论在Repository中使用EF框架

    最近在思考框架的事情,从Petshop的传统三层框架过渡到目前的DDD模式. 目前纠结的几个节点是: 1,EF这个ORM框架,有没有必要在 Repository 层封装一下,或者直接在 Service ...

  10. wojilu中的路由

    要看2个地方,一个是route.config,另一个是wojilu.Members.Sites.Domain.SiteMenu.config,这2部分综合起作用.