0

I'm working with some AngularJS ng-repeat looping, but I want to generate the markup of a grid, but need to add a 'grid--last' class to the 4th item in the grid, is this achievable?

My HTML:

<div ng-repeat="service in services" class="grid__4">
                <div class="services__box">
                    <img src="img/services/{{service.thumb}}.jpg" alt="" class="services__box__img">
                    <h1 class="services__box--alpha alpha">{{service.title}}</h1>
                    <p>
                        {{service.text}}
                    </p>
                    <a href="{{service.url}}.php" class="services__box--btn btn btn--blue">{{service.button}}</a>
                </div>
            </div>

Ideally I need to get the index of the array and use ng-class. Can anybody shed some light as to how I could do this? Thanks!

ng-class="{}"

2 Answers 2

2

Angular repeat scope provide an $index property; therefore, you can do data-ng-class="{'grid--last': $index == 3}" assuming you mean that 4th item is 3 since index start at 0.

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

4 Comments

Thanks! Just tried it, however it doesn't work :( can you advise on the HTML usage to make sure I'm doing it correctly? Thanks again.
Console error actually: Error: Syntax Error: Token '-' is unexpected, expecting [:] at column 6
Does work infact, but my hyphens aren't letting it work. Any ideas how I can fix that? :)
Sorry, I forgot about the hyphen. You could wrap it around single quote: 'grid--last': $index == 3
2

You can do this with CSS, but there may be browser compatibility issues with IE8 and lower (shocking)

plnk here

relevant code:

<style>
      li.item {
        background-color:blue;
      }

      li.item:last-child {
       background-color: red;
      }
    </style>
  </head>

  <body ng-controller="testCtl">
    <ul>
      <li class="item" ng-repeat="val in [1, 2, 3, 4]">{{val}}</li>
    </ul>
  </body>

Comments

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.