AngularJS 服務
在 AngularJS 中,您可以建立自己的服務,也可以使用許多內建服務之一。
什麼是服務?
在 AngularJS 中,服務是一個函式或物件,它可以供您的 AngularJS 應用程式使用,並且僅限於您的 AngularJS 應用程式。
AngularJS 擁有大約 30 個內建服務。其中之一是 $location
服務。
$location
服務具有可返回有關當前網頁位置資訊的函式。
示例
在控制器中使用 $location
服務
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
自己動手試一試 »
請注意,$location
服務作為引數傳遞給控制器。為了在控制器中使用該服務,必須將其定義為依賴項。
為什麼要使用服務?
對於許多服務,例如 $location
服務,您可能會認為可以使用 DOM 中已有的物件,如 window.location
物件,而您確實可以這樣做,但這會帶來一些限制,至少對您的 AngularJS 應用程式是這樣。
AngularJS 會持續監督您的應用程式,並且為了正確處理更改和事件,AngularJS 更傾向於您使用 $location
服務而不是 window.location
物件。
The $http Service
The $http
service is one of the most common used services in AngularJS applications. The service makes a request to the server, and lets your application handle the response.
示例
Use the $http
service to request data from the server
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
自己動手試一試 »
This example demonstrates a very simple use of the $http
service. Learn more about the $http
service in the AngularJS Http Tutorial.
The $timeout Service
The $timeout
service is AngularJS' version of the window.setTimeout
function.
示例
Display a new message after two seconds
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
}, 2000);
});
自己動手試一試 »
The $interval Service
The $interval
service is AngularJS' version of the window.setInterval
function.
示例
Display the time every second
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
},
1000);
});
自己動手試一試 »
Create Your Own Service
To create your own service, connect your service to the module
Create a service named hexafy
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
To use your custom made service, add it as a dependency when defining the controller
示例
Use the custom made service named hexafy
to convert a number into a hexadecimal number
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
自己動手試一試 »
Use a Custom Service Inside a Filter
Once you have created a service, and connected it to your application, you can use the service in any controller, directive, filter, or even inside other services.
To use the service inside a filter, add it as a dependency when defining the filter
The service hexafy
used in the filter myFormat
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
自己動手試一試 »
You can use the filter when displaying values from an object, or an array
<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
自己動手試一試 »