Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

From this link: http://voryx.net/using-angularjs-filter-inside-the-controller/

I expected back: Object1, Object5, Object8. Instead I get nothing. So how do I use $filter to filter out multiple values from one column?

I tried using this code below:

var myAppModule = angular.module('myApp', []);

myAppModule.controller('PageController', ["$scope", "$filter", function($scope, $filter) {
  // sample collection of objects
  var myObjects = [{
    name: "Object1",
    shape: "circle",
    color: "red"
  }, {
    name: "Object2",
    shape: "square",
    color: "orange"
  }, {
    name: "Object3",
    shape: "triangle",
    color: "yellow"
  }, {
    name: "Object4",
    shape: "circle",
    color: "green"
  }, {
    name: "Object5",
    shape: "sphere",
    color: "blue"
  }, {
    name: "Object6",
    shape: "hexagon",
    color: "indigo"
  }, {
    name: "Object7",
    shape: "square",
    color: "violet"
  }, {
    name: "Object8",
    shape: "triangle",
    color: "red"
  }];

  $scope.myRedObjects = $filter('filter')(myObjects, 
    [{color: "red"}, {color: "blue"}]);    
}]);

HTML:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class='container' ng-app="myApp" ng-controller='PageController'>
  <div class="page-header">
    <h1>AngularJS $filter in Controller <small>Subtext for header</small></h1>
  </div>

  <h5> // return an array with only red objects</h5>
  <code>
  $scope.myRedObjects = $filter('filter')(myObjects, { color: "red" });
  </code>
  <p ng-repeat="mro in myRedObjects">{{mro.name}}</p>
  <p></p>
  <p></p>
  <p>
    code taken from : http://voryx.net/using-angularjs-filter-inside-the-controller/
  </p>
</div>
</body>
</html>
share|improve this question
up vote 1 down vote accepted

Filter may use a callback:

$filter('filter')(myObjects, function(value, index, array){
    if(value.color === 'red' || value.color === 'blue') {
      return true;
    }    
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.