zoukankan      html  css  js  c++  java
  • Ⅳ.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)这些来调用,其中主要区别是getJSONjsonp的方法名称,以及几个不常用的方法$http.head$http.post$http.put$http.delete

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

    作者:cnljli
    欢迎转载,但还请尊重劳动果实,保留此段声明并注明原文链接。
  • 相关阅读:
    对 String 的几个错误认识 X
    用C# 自定义Window7的JumpList(跳转列表) X
    IPv6无状态自动配置功能配合DHCPv6无状态配置功能 实现IPv6自动分配
    H3C S7500E IPV6白皮书
    静默方式执行chkdsk命令
    IPv6基本知识(转载)
    解决win7官方主题themepack无法安装的问题
    英保通等PXE网刻软件的使用
    通过命令提示符修改windows默认打印机
    OFFICE2010出现两个激活信息的处理办法。
  • 原文地址:https://www.cnblogs.com/cnlj/p/3445125.html
Copyright © 2011-2022 走看看