標籤:tostring asc service com world customers 過濾器 筆記 welcome
在angularjs中,服務是一個函數或對象,可在angularjs應用中使用。其中$location服務可以返回當前頁面的url地址。要使用它,需要在controller中進行定義,作為一個參數傳入到controller中。
<!DOCTYPE html><html lang="en"><head><script src="../angular/angular.js"></script></head><body><p ng-app="myApp" ng-controller="customersCtrl">{{myUrl}}</p><script>var app = angular.module(‘myApp‘,[]);app.controller(‘customersCtrl‘,function($scope,$location){$scope.myUrl = $location.absUrl();});</script></body></html>
$http服務向伺服器發送請求,應用響應伺服器傳送過來的資料。
var app = angular.module("myApp",[]);app.controller("myCtrl",function($scope,$http){$http.get("welcome.html").then(function(response){$scope.myWelcome = response.data;});});
$timeout
var app = angular.module("myApp",[]);app.controller("myCtrl",function($scope,$timeout){$scope.myHeader = "hello world";$timeout(function(){$scope.myHeader = "how are you today?";},2000);});
建立自訂服務:可以建立自訂服務,並連結到自己的模組中,但在訪問自訂服務時,需要在定義控制器的時候獨立添加,設定依賴關係。當自訂服務串連到自己的應用上後,可以在控制器/指令/過濾器或其他服務中使用它。
app.service(‘hexafy‘,function(){this.myFunc = function(x){return x.toString(16);}});app.controller(‘myCtrl‘,function($scope,hexafy){$scope.hex = hexafy.myFunc(255);});app.filter(‘myFormat‘,[‘hexafy‘,function(hexafy){return function(x){return hexafy.myFunc(x);};}]);
未完待續。。。
angularjs學習筆記--服務