0

I have a simple repeater setup like so

<ion-item ng-repeat="location in data.posts">
  {{location.title}}
</ion-item>

And of course for every array I get a title inside an ion-item. Now for the tricky stuff.

data is coming from some JSON I'm pulling in. I want to access something deeper into the array but I don't know how to get it. In javascript I would access it as

location['custom_fields']['coupon_relationships']

Which coupon_relationships is another array for any amount of items. I merely want to be able to count the amount of items in there and print out the integer. Then I want to do logic so that the word coupons follows unless there is only 1 at which point I want the word to be coupon. I know I can do this all via javascript but I'm trying to learn the features of AngularJS. Is all this possible via Angular html?

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

2 Answers2

2

It's difficult to tell if this is exactly what you're interested in from your question, but you might try something like:

<ion-item ng-repeat="location in data.posts">
  {{location.title}} -
  {{location.custom_fields.coupon_relationships.length}}
  {{location.custom_fields.coupon_relationships.length != 1 ? 'Coupons' : 'Coupon'}}
</ion-item>
John Ledbetter
  • 13,557
  • 1
  • 61
  • 80
1

John's answer would be best for angular 1.1.5+ (that's when they added the ternary operator in templates)

If you are running an old version you could do something like this:

<div ng-app>
    <div ng-controller="PollCtrl">
        <li ng-repeat="poll in polls">
            <a href="#">
            {{poll.custom_fields.coupon_relationships.length}}
            Coupon<span ng-if="poll.custom_fields.coupon_relationships.length != 1">s</span>
            </a>
        </li>
    </div>
</div>

controller:

function PollCtrl($scope) {
    $scope.polls = [
        {
            custom_fields:
                {
                  coupon_relationships:
                     [
                       'one here', 'another','fffff','ffffff'
                     ]
                }
        }
     ]
}

codepen

Community
  • 1
  • 1