Patrioticcow
Patrioticcow

Reputation: 27038

how to deal with an if statement in a loop in AngularJS?

i have this 2 loops

    <ion-list>
      <ion-item ng-repeat="category in categories" href="#">
        {{category.name}} {{category.id}}
          <ion-item ng-repeat="test in tests" href="#">
              {{test.name}} {{test.cat_id}}
          </ion-item>
      </ion-item>
    </ion-list>

i want to test if(category.id === test.cat_id) then show the second list

i'm not sure if and how to use ngif, or if i should take care of this in the controller and return one object already formatted for my needs..

any ideas?

Upvotes: 1

Views: 4251

Answers (2)

Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

If I understand correctly you want to conditionally show the second ng-repeat. If that is the case then you can use this:

<ion-list>
  <ion-item ng-repeat="category in categories" href="#">
    {{category.name}} {{category.id}}
      <ion-item ng-repeat="test in tests" href="#" ng-if="category.id === test.cat_id">
          {{test.name}} {{test.cat_id}}
      </ion-item>
  </ion-item>
</ion-list>

Another option would be to use a filter like this:

<ion-list>
  <ion-item ng-repeat="category in categories" href="#">
    {{category.name}} {{category.id}}
      <ion-item ng-repeat="test in tests | filter: {cat_id: category.id}" href="#" ng-if="category.id === test.cat_id">
          {{test.name}} {{test.cat_id}}
      </ion-item>
  </ion-item>
</ion-list>

I would actually recommend the filter option.

Upvotes: 2

spacemigas
spacemigas

Reputation: 791

What about something like this?

<ion-list>
  <ion-item ng-repeat="category in categories" href="#">
    {{category.name}} {{category.id}}
      <ion-item ng-repeat="test in tests | filter:{cat_id: category.id}" href="#">
          {{test.name}} {{test.cat_id}}
      </ion-item>
  </ion-item>
</ion-list>

Upvotes: 0

Related Questions