日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

42.angularJS自定义服务

發(fā)布時間:2025/3/17 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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進(jìn)制是:</p> 13 14 <h1>{{hex}}</h1> 15 16 </div> 17 18 <p>自定義服務(wù),用于轉(zhuǎn)換16進(jìn)制數(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ù)

當(dāng)你創(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進(jìn)制轉(zhuǎn)換為16進(jìn)制。</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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。