我的过滤器不工作
问题描述:
我试图将我的状态值从数字更改为'文本'我的过滤器不工作
我想知道我该如何解决它,以及我做错了什么?预先感谢您
<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;
};
});
答
您正在控制器中定义一个过滤器。这是无效的。在应用程序启动并实例化控制器之前,您只能在配置阶段添加过滤器。代码应该是:
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;
});
});
它不应该是'返回statu'而不是点吗? – lucuma 2014-10-10 19:48:12
对不起,我错过了,但仍然不起作用 – adam 2014-10-10 19:50:00
打开控制台,有什么错误? – tymeJV 2014-10-10 19:51:47