0

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/

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.