AngularJS 服务(Service)
? ? ? AngularJS 中你可以创建自己的服务,或使用内建服务。 什么是服务?在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用。 AngularJS 内建了30 多个服务。 有个?$location?服务,它可以返回当前页面的 URL 地址。 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 <p> 当前页面的url:</p> 11 <h3>{{myUrl}}</h3> 12 </div> 13 14 <p>该实例使用了内建的 $location 服务获取当前页面的 URL。</p> 15 16 <script> 17 var app = angular.module(‘myApp‘,[]); 18 app.controller(‘myCtrl‘,function($scope,$location) { 19 $scope.myUrl = $location.absUrl(); 20 }); 21 </script> 22 23 </body> 24 </html> 注意?$location?服务是作为一个参数传递到 controller 中。如果要使用它,需要在 controller 中定义。 为什么使用服务?在很多服务中,比如 $location 服务,它可以使用 DOM 中存在的对象,类似 window.location 对象,但 window.location 对象在 AngularJS 应用中有一定的局限性。 AngularJS 会一直监控应用,处理事件变化, AngularJS 使用?$location?服务比使用?window.location?对象更好。 $location vs window.location
$http 服务$http?是 AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。 实例使用?$http?服务向服务器请求数据: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 11 <p>欢迎信息:</p> 12 13 <h1>{{myWelcome}}</h1> 14 15 </div> 16 17 <p> $http 服务向服务器请求信息,返回的值放入变量 "myWelcome" 中。</p> 18 19 <script> 20 var app = angular.module(‘myApp‘,[]); 21 app.controller(‘myCtrl‘,$http) { 22 $http.get("welcome.htm").then(function (response) { 23 $scope.myWelcome = response.data; 24 }); 25 }); 26 </script> 27 28 </body> 29 </html> 以上是一个非常简单的?$http?服务实例,更多?$http?服务应用请查看?AngularJS Http 教程。 $timeout 服务AngularJS?$timeout?服务对应了 JS?window.setTimeout?函数。 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 11 <p>两秒后显示信息:</p> 12 13 <h1>{{myHeader}}</h1> 14 15 </div> 16 17 <p>$timeout 访问在规定的毫秒数后执行指定函数。</p> 18 19 <script> 20 var app = angular.module(‘myApp‘,$timeout) { 22 $scope.myHeader = "Hello World!"; 23 $timeout(function () { 24 $scope.myHeader = "How are you today?"; 25 },2000); 26 }); 27 </script> 28 29 </body> 30 </html> $interval 服务AngularJS?$interval?服务对应了 JS?window.setInterval?函数。 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 11 <p>现在时间是:</p> 12 13 <h1>{{theTime}}</h1> 14 15 </div> 16 17 <p>$interval 访问在指定的周期(以毫秒计)来调用函数或计算表达式。</p> 18 19 <script> 20 var app = angular.module(‘myApp‘,$interval) { 22 $scope.theTime = new Date().toLocaleTimeString(); 23 $interval(function () { 24 $scope.theTime = new Date().toLocaleTimeString(); 25 },1000); 26 }); 27 </script> 28 29 </body> 30 </html> 创建自定义服务你可以创建自定义服务,链接到你的模块中: ? 创建名为hexafy?的服务: app.service(‘hexafy‘,function() { this.myFunc = function (x) { return x.toString(16); } }); 要使用自定义服务,需要在定义控制器的时候独立添加,设置依赖关系:. 实例使用自定义的的服务?hexafy?将一个数字转换为16进制数: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 <div ng-app="myApp" ng-controller="myCtrl"> 9 10 <p>255 的16进制是:</p> 11 12 <h1>{{hex}}</h1> 13 14 </div> 15 16 <p>自定义服务,用于转换16进制数:</p> 17 18 <script> 19 var app = angular.module(‘myApp‘,[]); 20 21 app.service(‘hexafy‘,function() { 22 this.myFunc = function (x) { 23 return x.toString(16); 24 } 25 }); 26 app.controller(‘myCtrl‘,hexafy) { 27 $scope.hex = hexafy.myFunc(255); 28 }); 29 </script> 30 31 </body> 32 </html> 过滤器中,使用自定义服务当你创建了自定义服务,并连接到你的应用上后,你可以在控制器,指令,过滤器或其他服务中使用它。 在过滤器?myFormat?中使用服务?hexafy: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp"> 10 在过滤器中使用服务: 11 12 <h1>{{255 | myFormat}}</h1> 13 14 </div> 15 16 <script> 17 var app = angular.module(‘myApp‘,[]); 18 app.service(‘hexafy‘,function() { 19 this.myFunc = function (x) { 20 return x.toString(16); 21 } 22 }); 23 app.filter(‘myFormat‘,[‘hexafy‘,function(hexafy) { 24 return function(x) { 25 return hexafy.myFunc(x); 26 }; 27 }]); 28 29 </script> 30 31 </body> 32 </html> ? 在对象数组中获取值时你可以使用过滤器: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 <p>在获取数组 [255,251,200] 值时使用过滤器:</p> 11 12 <ul> 13 <li ng-repeat="x in counts">{{x | myFormat}}</li> 14 </ul> 15 16 <p>过滤器使用服务将10进制转换为16进制。</p> 17 </div> 18 19 <script> 20 var app = angular.module(‘myApp‘,[]); 21 app.service(‘hexafy‘,function() { 22 this.myFunc = function (x) { 23 return x.toString(16); 24 } 25 }); 26 app.filter(‘myFormat‘,function(hexafy) { 27 return function(x) { 28 return hexafy.myFunc(x); 29 }; 30 }]); 31 app.controller(‘myCtrl‘,function($scope) { 32 $scope.counts = [255,251,200]; 33 }); 34 </script> 35 36 </body> 37 </html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |