1

I am working with angularjs ng-repeat. I have this code:

angular.module('app', [])
  .controller('MainCtrl', function($scope) {
    var items = [1,2,3,4,5,6,7];
    $scope.items = items;
  });

I want to have html result like this:

<ul>
  <li>
    <a href="">1</a>
    <a href="">2</a>
  </li>
  <li>
    <a href="">3</a>
    <a href="">4</a>
  </li>
  <li>
    <a href="">5</a>
    <a href="">6</a>
  </li>
  <li>
    <a href="">7</a>
  </li>
</ul>

The li element needs to be automatically generated as well. I have tried to use ng-repeat, but I am having difficulty with nested ng-repeat.

5
  • 1
    Please include your most promising attempt, including the ng-repeat. It's very helpful for us to see what you've tried. It allows us to better understand the exact problems you're running into. Commented Jun 12, 2015 at 20:44
  • you can use $index inside of ng-repeat to get the iterator offset. You could probably use that in combination with the mod % operator to build that HTML Commented Jun 12, 2015 at 20:46
  • 2
    While $index is an option, I prefer keeping this kind of logic in the angular controller (javascript). I'd turn the array into an array of pairs first, then ng-repeat on that array of pairs. Commented Jun 12, 2015 at 20:49
  • @Brian Glaz Thanks. Could you tell me more about creating the offset, I think this is exactly what I want to achieve. Commented Jun 12, 2015 at 21:06
  • @ryanyuyu, thanks for tip. I will think about it. Commented Jun 12, 2015 at 21:06

1 Answer 1

2

You need to split you array in multiple arrays

items = [[1,2], [3,4], [5,6], [7]];

And this new array you can bind to html

<ul>
    <li ng-repeat="li in items"> 
        <a href="" ng-repeat="atags in li">{{atags}}</a>
    </li>
</ul>

I've created you jsfiddle take a look: http://jsfiddle.net/k65wmddL/

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

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.