Alternating ngRepeat results in AngularJS

0

I have a blog whose content is completed through an AngularJS driver that brings the data from a base.

On the other hand, I have an object that contains a list of advertisements that are displayed in image format.

The drawback is when I intend to show 2 ads every 4 posts. I do not know how to make all ads appear. As it is now, the first 2 are always shown. I think the key is in "limitTo" but I manage to make the loop start from the last ad shown.

The code I am currently using is something like this:

<div ng-controller="noticias">
  <div ng-repeat-start="noticia in noticias">
    <h1>{{ noticia.titulo }}</h1>
    <p>{{ noticia.asunto }}</p>
  </div>
  <div ng-repeat-end ng-if="($index+1) % 4 ===0">
    <div ng-repeat="anuncio in anuncios | limitTo: 2>
      <img ng-src="anuncios/{{ anuncio.nombre }}
    </div>
  </div>
</div>
    
asked by Marcelo Forclaz 09.04.2017 в 20:28
source

2 answers

1

LimitTo supports a second parameter that is Start, indicates to which position of the array the limit is applied.

In your particular case, you could use something like this:

<div ng-repeat="anuncio in anuncios | limitTo: 2:($index+1)/2-2">

In this plunkr, you can see it working:

link

I hope this is what you are looking for.

    
answered by 14.04.2017 / 22:19
source
2

Excellent KN_ solution, for better understanding the expression could be improved as well

<div ng-repeat="anuncio in anuncios | limitTo: 2:(($index+1)/2)-2">

This way the parentizado is better understood and it is known that operation is being performed, it is well that we know which is the order of priority that the operations in JS are executed, but it is not necessary to place the paracentesis for greater readability and understanding of the code ,

Greetings, great contribution.

    
answered by 17.04.2017 в 19:31