zoukankan      html  css  js  c++  java
  • angularJS(三):服务(Service)、http

    一、服务


     服务是一个函数或对象,可在你的 AngularJS 应用中使用。

    可以创建自己的服务,或使用内建服务

     $location

    注意 $location 服务是作为一个参数传递到 controller 中。如果要使用它,需要在 controller 中定义。

    AngularJS 会一直监控应用,处理事件变化, AngularJS 使用 $location 服务比使用 window.location 对象更好。

    $http 服务

    $http 是 AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    
    <div ng-app="myApp" ng-controller="myCtrl"> 
    
    <p>欢迎信息:</p>
    
    <h1>{{myWelcome}}</h1>
    
    </div>
    
    <p> $http 服务向服务器请求信息,返回的值放入变量 "myWelcome" 中。</p>
    
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
      $http.get("welcome.htm").then(function (response) {
          $scope.myWelcome = response.data;
      });
    });
    </script>
    
    </body>
    </html>
    View Code

    $timeout 服务

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    
    <div ng-app="myApp" ng-controller="myCtrl"> 
    
    <p>两秒后显示信息:</p>
    
    <h1>{{myHeader}}</h1>
    
    </div>
    
    <p>$timeout 访问在规定的毫秒数后执行指定函数。</p>
    
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $timeout) {
      $scope.myHeader = "Hello World!";
      $timeout(function () {
          $scope.myHeader = "How are you today?";
      }, 2000);
    });
    </script>
    
    </body>
    </html>
    View Code

    $interval 服务

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    
    <div ng-app="myApp" ng-controller="myCtrl"> 
    
    <p>现在时间是:</p>
    
    <h1>{{theTime}}</h1>
    
    </div>
    
    <p>$interval 访问在指定的周期(以毫秒计)来调用函数或计算表达式。</p>
    
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $interval) {
      $scope.theTime = new Date().toLocaleTimeString();
      $interval(function () {
          $scope.theTime = new Date().toLocaleTimeString();
      }, 1000);
    });
    </script>
    
    </body>
    </html>
    View Code

    创建自定义服务

    app.service('hexafy', function() {
        this.myFunc = function (x) {
            return x.toString(16);
        }
    });

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myCtrl">
    
    <p>255 的16进制是:</p>
    
    <h1>{{hex}}</h1>
    
    </div>
    
    <p>自定义服务,用于转换16进制数:</p>
    
    <script>
    var app = angular.module('myApp', []);
    
    app.service('hexafy', function() {
        this.myFunc = function (x) {
            return x.toString(16);
        }
    });
    app.controller('myCtrl', function($scope, hexafy) {
      $scope.hex = hexafy.myFunc(255);
    });
    </script>
    
    </body>
    </html>
    View Code
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    
    <div ng-app="myApp">
    在过滤器中使用服务:
    
    <h1>{{255 | myFormat}}</h1>
    
    </div>
    
    <script>
    var app = angular.module('myApp', []);
    app.service('hexafy', function() {
        this.myFunc = function (x) {
            return x.toString(16);
        }
    });
    app.filter('myFormat',['hexafy', function(hexafy) {
        return function(x) {
            return hexafy.myFunc(x);
        };
    }]);
    
    </script>
    
    </body>
    </html>
    View Code
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    
    <div ng-app="myApp" ng-controller="myCtrl">
    <p>在获取数组 [255, 251, 200] 值时使用过滤器:</p>
    
    <ul>
      <li ng-repeat="x in counts">{{x | myFormat}}</li>
    </ul>
    
    <p>过滤器使用服务将10进制转换为16进制。</p>
    </div>
    
    <script>
    var app = angular.module('myApp', []);
    app.service('hexafy', function() {
        this.myFunc = function (x) {
            return x.toString(16);
        }
    });
    app.filter('myFormat',['hexafy', function(hexafy) {
        return function(x) {
            return hexafy.myFunc(x);
        };
    }]);
    app.controller('myCtrl', function($scope) {
        $scope.counts = [255, 251, 200];
    });
    </script>
    
    </body>
    </html>
    View Code

     二、Http

    使用格式:

    // 简单的 GET 请求,可以改为 POST
    $http({
    	method: 'GET',
    	url: '/someUrl'
    }).then(function successCallback(response) {
    		// 请求成功执行代码
    	}, function errorCallback(response) {
    		// 请求失败执行代码
    });

    POST 与 GET 简写方法格式:

    $http.get('/someUrl', config).then(successCallback, errorCallback);
    $http.post('/someUrl', data, config).then(successCallback, errorCallback);
    • $http.get
    • $http.head
    • $http.post
    • $http.put
    • $http.delete
    • $http.jsonp
    • $http.patch
     
  • 相关阅读:
    POJ3613 Cow Relays 经过n条边的最短路
    UML笔记(六)
    UML主要内容及参考资料
    UML笔记(五)
    UML笔记(一)
    UML笔记(三)
    UML笔记(四)
    软件工程——第十一章 软件项目管理
    软件工程——第十章 软件工程管理
    UML笔记(二)
  • 原文地址:https://www.cnblogs.com/web520/p/7239601.html
Copyright © 2011-2022 走看看