<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp">
<p>使用自定义服务在自定义过滤器中将数字 255 进行转换:</p>
<h1>{{255 | myFormat}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16); // 将数字转换为16进制字符串
}
});
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x); // 调用服务中的函数进行转换
};
}]);
</script>
</body>
</html>