當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
42.angularJS自定义服务
生活随笔
收集整理的這篇文章主要介紹了
42.angularJS自定义服务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)自:https://www.cnblogs.com/best/tag/Angular/
1.
你可以創(chuàng)建自定義服務(wù),鏈接到你的模塊中:
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="utf-8"> 6 <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> 7 </head> 8 9 <body> 10 <div ng-app="myApp" ng-controller="myCtrl"> 11 12 <p>255 的16進制是:</p> 13 14 <h1>{{hex}}</h1> 15 16 </div> 17 18 <p>自定義服務(wù),用于轉(zhuǎn)換16進制數(shù):</p> 19 20 <script> 21 var app = angular.module('myApp',[]); 22 app.service('hexafy',function(){ 23 this.myFunc = function(x){ 24 return x.toString(16); 25 } 26 }); 27 app.controller('myCtrl',function($scope,hexafy){ 28 $scope.hex = hexafy.myFunc(255); 29 }); 30 </script> 31 32 </body> 33 34 </html>?
?
2.
過濾器中,使用自定義服務(wù)
當你創(chuàng)建了自定義服務(wù),并連接到你的應(yīng)用上后,你可以在控制器,指令,過濾器或其他服務(wù)中使用它。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> 6 </head> 7 <body> 8 9 <div ng-app="myApp" ng-controller="myCtrl"> 10 <p>在獲取數(shù)組 [255, 251, 200] 值時使用過濾器:</p> 11 12 <ul> 13 <li ng-repeat="x in counts">{{x | myFormat}}</li> 14 </ul> 15 16 <p>過濾器使用服務(wù)將10進制轉(zhuǎn)換為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',['hexafy', 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>?
轉(zhuǎn)載于:https://www.cnblogs.com/sharpest/p/8176873.html
總結(jié)
以上是生活随笔為你收集整理的42.angularJS自定义服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java集合集锦
- 下一篇: java.lang.ClassNotFo