How to increment the counter with ng-repeat?

0
<div ng-repeat="column in columns">
    <div ng-if="column.new">
        {{ contador = contador + 1 }}
        <span ng-if="contador === 1">
            <div class="alert alert-danger">TESTDASDALGO</div>
        </span>
        <span ng-if="contador > 1">
            {{ contador = 0 }}
        </span>
    </div>
</div>

The values are being printed between colchetes .

Another question:

  • Should logic be done on the front? I have a lot of conditional on my project, it will probably be pretty confusing ... (Is there another way to do it?)

Is it possible to do something like the one I wrote below?

var contador = 0;
angular.forEach($scope.columns, function(column){
    if(column.new){
        contador++;
        if(contador === 1){
            include('umarquivo.html');
        }
        if(contador > 1){
            '</div>'; //fechar uma div
            contador = 0;
        }
    }
});
    
asked by anonymous 15.02.2017 / 18:01

2 answers

1

There is no need to perform such an operation just for a row counter, since ng-repeat provides one automatically - $index :

angular.module('myApp', [])
.controller('myController', function($scope){
  $scope.paineis = ['aaaaa','bbbbb','ccccc','ddddd','eeeee','fffff']; 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><divng-app="myApp">
  <div ng-controller='myController'>
    <div ng-repeat='i in paineis'>{{$index}} - {{i}}</div>
  </div>
</div>
    
15.02.2017 / 18:21
0

Hello,

ng-repeate provides the iteration index, so you can use this variable to help you solve your problem.

If you want to better understand how ng-repeat works link

<div ng-repeat="column in columns">
   <div ng-if="column.new">
       <span ng-if="$index === 1">
           <div class="alert alert-danger">TESTDASDALGO</div>
       </span>
    <span ng-if="$index > 1">
        {{ contador = 0 }}
    </span>
 </div>
</div>

Always avoid placing business logic in the view, it is responsible for the representation of information for the user, always try to put logic of views in the controllers

    
15.02.2017 / 18:22