angularjs學習筆記--服務

來源:互聯網
上載者:User

標籤: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學習筆記--服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.