2014-10-10 106 views
-1

我试图将我的状态值从数字更改为'文本'我的过滤器不工作

我想知道我该如何解决它,以及我做错了什么?预先感谢您

 <table> 
     <tr ng-repeat="x in Id"> 
       <td>{{ x.id }}</td> 
       <td>{{ x.status | status}}</td> 
      </tr> 
     </table> 

当我写|状态< - 崩溃整个重复,没有任何显示。

var app = angular.module('customersController', []); 

    function customersController($scope, $http) { 

    $http.get("localhost") 
      .success(function (response) { 
       $scope.Id = response; 

      }); 

    app.filter('status', function() { 
     return function (input) { 
      var statu; 
      switch (input) { 
       case 10: 
        statu = 'Bronze'; 
        break; 
       case 20: 
        statu = 'Silver'; 
        break; 
       case 30: 
        statu = 'Gold'; 
        break; 
       case 40: 
        statu = 'Elite'; 
        break; 

      } 

      return statu; 
     }; 
    }); 
+1

它不应该是'返回statu'而不是点吗? – lucuma 2014-10-10 19:48:12

+0

对不起,我错过了,但仍然不起作用 – adam 2014-10-10 19:50:00

+0

打开控制台,有什么错误? – tymeJV 2014-10-10 19:51:47

回答

1

您正在控制器中定义一个过滤器。这是无效的。在应用程序启动并实例化控制器之前,您只能在配置阶段添加过滤器。代码应该是:

var app = angular.module('customersController', []); 
app.filter('status', function() { 
    return function (input) { 
     var statu; 
     switch (input) { 
      case 10: 
       statu = 'Bronze'; 
       break; 
      case 20: 
       statu = 'Silver'; 
       break; 
      case 30: 
       statu = 'Gold'; 
       break; 
      case 40: 
       statu = 'Elite'; 
       break; 
     } 
     return statu; 
    }; 
}); 

app.controller('customersController', function($scope, $http) { 
    $http.get("localhost") 
     .success(function (response) { 
      $scope.Id = response; 
     }); 
}); 
+0

如果我做对了,你可以检查我的运动员吗? – adam 2014-10-10 20:16:51

+0

呃,哪个闯入? – 2014-10-10 20:24:49

+0

http://plnkr.co/edit/boCZMLqWq05vCz9K3c8w?p=preview – adam 2014-10-10 20:25:23