Angularjs总结(二)过滤器使用

html页面:

 1 <table>
 2     <thead>
 3         <tr>
 4             <td class="td">序号</td>
 5             <td class="td ">规划用途</td>
 6         </tr>
 7     </thead>
 8     <tbody>
 9         <tr ng-repeat="FW in FWlist ">
10             <td class="td" ng-bind="$index+1">1</td>
11             <td class="td ">{{FW.GHYT|NAME}}</td>
12         </tr>
13     </tbody>
14 </table>

过滤器:

 1 define(['angular'], function (ng) {
 2     'use strict';
 3     ng.module('index-filters', [])
 4 
 5         .filter('price', [function () {
 6 
 7             return function (num) {
 8 
 9                 return '¥ ' + num;
10 
11             };
12 
13         }])
14         .filter('gender', [function () {
15 
16             return function (gender) {
17 
18                 switch (gender) {
19 
20                     case 1: return '男';
21 
22                     case 2: return '女';
23 
24                     case 0: return '';
25 
26                 }
27 
28             }
29 
30         }])
31         .filter('NAME', [function () {
32 
33             return function (YWLX) {
34 
35                 if (YWLX == null) return "";
36 
37 
38                 var str = YWLX;
39 
40                 if (YWLX.length > 10) {
41 
42                     str = YWLX.substring(0, 10) + "...";
43 
44                 }
45                 return str;
46 
47             }
48 
49         }])
50         .filter('stateimgurl', [function () {
51 
52             return function (state) {
53 
54                 switch (state) {
55 
56                     case '正常': return '../img/normal.png';
57 
58                     case '预警': return '../img/warning.PNG';
59 
60                     case '超期': return '../img/timeout.png';
61                 }
62 
63             }
64 
65         }])
66         .filter('GHYTToName', ['audit-service', function (auditservice) {
67             //audit-service  服务
68             var tempData = [];
69             auditservice.getParameter("房屋用途").success(function (sjlxlist) {
70 
71                 tempData = sjlxlist;
72 
73             });
74 
75             return function (id) {
76                 for (var i = 0; i < tempData.length; i++) {
77 
78                     if (tempData[i].Code == id) {
79 
80                         return tempData[i].Name;
81                     }
82                 }
83             }
84         }])
85 })

html页面所需的控制器:

1 define(['index-filters'], function (app) {
2     app.controller('index-controller', ['$rootScope', '$scope', '$location', '$cookies',
3        function ($rootScope, $scope, $location, $cookies) {
4 
5        }])
6 })
原文地址:https://www.cnblogs.com/bobo-show/p/5632830.html