0

I want to filter an array in a controller but it returns nothing.

This is my controller:

.controller('cardsCtrl', function($scope, $sce, LearnCard, Category, $ionicModal, $stateParams, $filter) {

  console.log("stateParam: " + $stateParams.catId);

  // Get all LearnCards
  $scope.learnCardsAll = LearnCard.query();
  console.log("Before filtering");
  console.log($scope.learnCardsAll); // is populated

  $scope.learnCards = $filter('filter')($scope.learnCardsAll, { category: 1 });

learnCardsAll is populated as it should.

This is the structure of the elements: Console log

Maybe someone could help me?

2
  • Have you tried using lodash's _.filter method? Commented Dec 30, 2015 at 21:03
  • If you can see that $scope.learnCardsAll is populated, then $scope.learnCards = _.filter($scope.learnCardsAll, {category: 1}); should work; Commented Dec 30, 2015 at 21:10

1 Answer 1

2

The .query method is async and takes a callback.

Try to do your filtering inside of it.

LearnCard.query(function(cards) {
  $scope.learnCards = $filter('filter')(cards, { category: 1 });
});

https://docs.angularjs.org/api/ngResource/service/$resource

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

1 Comment

Thank you! That solves my problem... somehow. The view takes the filtered list. There is so much new stuff to read through and input so I did not think of that.

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.