AngularJS - 过滤特定的深层属性

时间:2015-07-07 10:11:55

标签: javascript angularjs

我有一个javascript对象,想在特定的深属性上使用angular的过滤器,但它似乎不起作用。

function MyCtrl($scope, $filter)
{
 $scope.items = [
    {id:{val:1,str:'abc'}, name:'John'},
    {id:{val:2,str:'abcd'}, name:'John'},
    {id:{val:3,str:'xyz'}, name:'John'},
    {id:{val:4,str:'axcvb'}, name:'John'},
    {id:{val:5,str:'qwe'}, name:'John'}];

 $scope.items2 = $scope.items;

 $scope.$watch('search', function(param)
 {
    $scope.items = $filter('filter')($scope.items2, {id:{val:param}},false);
 });
};

这里是小提琴http://jsfiddle.net/Lhhetazm/2/

如果我只是说,

$scope.items = $filter('filter')($scope.items2, {id:param},false);

然后它工作,但它搜索整个对象不是我想要的,我希望它适用于部分搜索,所以我将完全匹配标志设置为false

1 个答案:

答案 0 :(得分:0)

从Controller中删除$watch并对ng-repeat ...

执行过滤
<tr ng-repeat="item in items | filter:{id.val:search}">
    <td>{{item.id}}</td>
    <td>{{item.name}}</td>
</tr>