简体   繁体   English

AngularJS-过滤特定的深色属性

[英]AngularJS - filter on a specific deep property

I have a javascript object and want to use angular's filter on a specific deep property but it does not seem to work. 我有一个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);
 });
};

here is the fiddle http://jsfiddle.net/Lhhetazm/2/ 这是小提琴http://jsfiddle.net/Lhhetazm/2/

If i just say, 如果我只是说,

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

Then it works but it searches the whole object which is not what i want, and i want it to work for partial searchs so i set the exact match flag to false 然后它起作用了,但是它搜索了不是我想要的整个对象,并且我希望它可以用于部分搜索,因此我将完全匹配标志设置为false

Get rid of the $watch from the Controller and perform the filter on the ng-repeat ... 从Controller摆脱$watch并在ng-repeat上执行过滤器...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM