2014-10-10 130 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