mercredi 19 avril 2017

AngularJS use one object as a filter for another

I'm trying to filter my ng-repeat through a set of checkboxes which come from a different object. Object 1 holds my categories and object 2 holds all my articles.

The categores object will turn into checkboxes. These checkboxes should act as filter for the articles. An article can have mutliple categories.

$scope.categories:

[
  {
    "id": "1",
    "title": "Blog"
  },
  {
    "id": "2",
    "title": "News"
  }
]

$scope.articles:

[
  {
    "id": "1",
    "title": "Whats going on",
    "categories":{
     "results" : [1,2,3]
    }
  },
  {
    "id": "2",
    "title": "Trump!",
    "categories":{
     "results" : [3]
    }
  }
]

Checkboxes:

<div class="filter-pills" ng-repeat="cat in categories">
   <input type="checkbox" ng-model="filter[cat.Id]" ng-checked="cat.checked"/>                         
</div>

ng-repeat:

<div class="col-lg-3 col-md-4" ng-repeat="item in articlesFinal"></div>

I have tried different solutions like ng-change when i update my filter array and compare it to the object used in ng-repeat.

I can't seem to figure this one out. Any suggestions?




Aucun commentaire:

Enregistrer un commentaire