Madhu
Madhu

Reputation: 1019

Filter on a specific property from code using angularjs built-in filter

How can we filter based on a specific property for the built-in filter, from code? I know how to do it from markup -

<li ng-repeat="user in users | filter:{status:status}">{{user.name}}</li>

This seems to be a particular problem when the property name is a variable. I've recreated the problem in jsfiddle - http://jsfiddle.net/n925b20L/

Upvotes: 0

Views: 45

Answers (1)

dfsq
dfsq

Reputation: 193261

You should use bracket notation if you need to construct an object with a dynamic key stored in variable:

$scope.$watch('status', function(){
    var filter = {};
    filter[$scope.filterby.val] = $scope.status;
    $scope.filtered = $filter('filter')($scope.users, filter);
    console.log($scope.filtered);
});

Demo: http://jsfiddle.net/n925b20L/2/

Upvotes: 2

Related Questions