案例:AngularJS案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<script src="/js/angular.js1.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',['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>
38