服務是一個函數或對象,可在你的 AngularJS 應用中使用。css
能夠建立本身的服務,或使用內建服務html
$locationjson
注意 $location 服務是做爲一個參數傳遞到 controller 中。若是要使用它,須要在 controller 中定義。數組
AngularJS 會一直監控應用,處理事件變化, AngularJS 使用 $location 服務比使用 window.location 對象更好。服務器
$http 服務app
$http 是 AngularJS 應用中最經常使用的服務。 服務向服務器發送請求,應用響應服務器傳送過來的數據。ide
<!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>
$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>
$interval 服務post
<!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>
建立自定義服務jsonp
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>
<!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>
<!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>
2、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);