andre-3000
andre-3000

Reputation: 73

AngularJS filter array in controller

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?

Upvotes: 0

Views: 932

Answers (1)

jcreamer898
jcreamer898

Reputation: 8189

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

Upvotes: 2

Related Questions