1

I have the array of objects. when I require to filter the object by single vaue i am doing like this:

$scope.filteredByPhase = $filter('filter')($scope.allApps, {Phase:"All"});
$scope.allAppsBatch = $scope.filteredByPhase;

But as a option, I would like to filter the objects by 2 'Phase` values by "All" or "Home" in this case how to filter?

I tried like this:

$scope.filteredByPhase = $filter('filter')($scope.allApps, {Phase:("All" || "Home")});
$scope.allAppsBatch = $scope.filteredByPhase;

But not works.. any one guide me please?

2 Answers 2

2

In AngularJS, you can use a function as an expression in the filter. In the function you can validate the condition and return Boolean value. All the falsy items are filtered out of the result. So you can do

$scope.filteredByPhase = $filter('filter')($scope.allApps, function (app) {
    if (app.Phase == "All" || app.Phase == "Home") {
        return true;
    }
    return false;
});

Read More : AngularJS Filter Documentation

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

Comments

0

Use $filter passing an anonymous comparison function.

$scope.filteredItems = $filter('filter')($scope.items, function (item) {
  return (item.Phase == "all") ? true : false;
});

Keep in mind that you may use Array.filter as well:

$scope.items = [{
  Phase: "home"
}, {
  Phase: "all"
}, {
  Phase: "all"
}, {
  Phase: "home"
}];
console.log($scope.items);
$scope.filteredItems = $scope.items.filter(function (item) {
  return (item.Phase == "all") ? true : false;
})
console.log($scope.filteredItems)

You may also trigger multiple filtering actions using chaining:

$scope.fi = $scope.i.filter(func1).filter(func2);

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.