Skip to content Skip to sidebar Skip to footer

Angularjs Foreach Only Returns One Object

This could be something simple and I'm overlooking it. But I'm building out a filter by categories and once a user clicks a category it updates a scope (my instance $scope.productS

Solution 1:

$scope.update = function(val) {
  // Create an empty arrayvar stuff = [];
  angular.forEach($scope.productStuff, function(item){
    if( item.s2 === val.toUpperCase() ){
      // push to our array when condition is met (filter)
      stuff.push(item);
    }       
  });
  // $scope.productStuff now contains all the filtered items$scope.productStuff = stuff;
}

Solution 2:

You are trying to modify iterate over and modifying $scope.productStuff too. As soon as you write:

$scope.productStuff = [item];

only one item remains in it. try creating a new array and once done assign it to $scope.productStuff

$scope.update = function(val) {
  var tempArray = [];
  angular.forEach($scope.productStuff, function(item){
    if( item.s2 === val.toUpperCase()){
      tempArray.push(item);
    }       
  });
  $scope.productStuff = tempArray;
}

Post a Comment for "Angularjs Foreach Only Returns One Object"