angularjs ng-repeat过滤静态值不起作用

时间:2014-07-28 17:07:13

标签: angularjs angularjs-ng-repeat

我无法弄清楚为什么下面的代码不会仅使用unique_id为“027”的值进行过滤。我已经尝试过它作为一个字符串'027'......

JSON文件:

[
  {
    "unique_id":"027",
    "title":"How Speed Relates to Energy",
    "state":"NY",
    "state_id":"1.S2.3a"
  }
]

这是我的控制器:

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

abApp.factory('abData', function($http, $q) {
var deffered = $q.defer();
var data = [];
var abData = {};

abData.async = function() {
    $http.get('/data/ab_activities.json')
        .success(function(ab) {
            data = ab;
            deffered.resolve();
        });
    return deffered.promise;
};
abData.data = function() {
    return data;
};
return abData;
});


abApp.controller("abEE", function(abData, $scope) {
    var abApp = this;
    abData.async().then(function(ab) {
        abApp.alignments = abData.data();
    });

})

这是我的HTML:

<div ng-controller="abEE as ee">
    <ul>
        <li ng-repeat="align in ee.alignments | filter:{unique_id : 027} ">
            {{align.unique_id}} - {{align.state}}, {{align.state_id}}
        </li>
    </ul>
 </div>

1 个答案:

答案 0 :(得分:1)

你需要像这样纠正你的html标记,因为你的JSON unique_id是一个字符串:

<div ng-controller="abEE as ee">
<ul>
    <li ng-repeat="align in ee.alignments | filter:{unique_id : '027'} ">
        {{align.unique_id}} - {{align.state}}, {{align.state_id}}
    </li>
</ul>