Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is modulus possible with ng-if/ng-repeat?

Using an Angular ng-repeat, I'm trying to create a carousel with 3 <div>s in each <li>. I can easily create it with 1 div per slide but can't manage to get 3. With js, I'd normally use a modulus (%) to figure out if the index is divisible by 3, and then open/close the li there.

Is this possible to do with Angular?

This is what I have (1 item per slide):

<ul carousel class="carousel">
  <li class="slide" ng-repeat="item in item.list.items" ng-init="itemIndex = $index">
    <div class="item">{{item}}</div>
  </li>
</ul>

This is what I'm trying to achieve (3 items per slide):

<ul class="carousel">

  <!-- slide 1, with 3 items -->
  <li class="slide">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
  </li>

  <!-- slide 2, with 3 items -->
  <li class="slide">
    <div class="item">Item 4</div>
    <div class="item">Item 5</div>
    <div class="item">Item 6</div>
  </li>

  <!-- and so on... -->
</ul>

Edit

This question was marked as duplicate by isherwood. The question very clearly asks about using modulus within ng-if, not controllers. The suggested duplicate is close, but Betty St answers the exact question below, with a code sample and link. Thanks Betty!

like image 620
shackleton Avatar asked Nov 05 '15 19:11

shackleton


People also ask

What does ng-repeat do?

The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

What can I use instead of NG-repeat?

But ng-repeat is not the right thing to use when you have large datasets as it involves heavy DOM manipulations. And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do you use two ng-repeat in a table?

NEST TWO ng-repeatThe first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection. By using one ng-repeat for the row and an other for the column, the rows and the columns of your table is now driven by a subset.

Where is the last element in NG-repeat?

$first and $last It's common when using ng-repeat to add specific behavior to the first or last element of the loop, e.g. special styling around the edges. Instead, ng-repeat already supplies you with two ready boolean properties. $first is true for the first element, and $last is true for the last element.


2 Answers

You can use % or groupBy. I always use modulus as described here: https://stackoverflow.com/a/25838091/595152

Your solution should look like this:

<ul carousel class="carousel">
  <li class="slide" ng-repeat="_ in item.list.items" ng-if="$index % 3 == 0">
    <div class="item" ng-repeat="i in [$index, $index + 1, $index + 2]" ng-init="item = item.list.items[i]">
      <div ng-if="item">{{item}}</div>
    </div>
  </li>
</ul>

You need to add a div with ng-if inside the div.item to make sure the item exists ;)

like image 151
Betty St Avatar answered Sep 23 '22 17:09

Betty St


Take a look at the groupBy filter to group the items into 3 groups.

https://github.com/a8m/angular-filter#groupby

like image 38
Dan Avatar answered Sep 22 '22 17:09

Dan