totothegreat
totothegreat

Reputation: 1643

How to skip $index in ng-repeat

Consider the following:

   <div ng-repeat='quest in quests'>
                        <div ng-switch on="quest.ui.type">
                            <div ng-switch-when="ms-select-single" >
                                <div ms-select-single quest='quest'
                                 quest-num='{{(true)?numInc():return}}'></div>
                            </div>
                            <div ng-switch-when="ms-select-multy">
                                <div ms-select-multy quest='quest'
                                quest-num='{{(true)?numInc():return}}'></div>
                            </div>
                            <div ng-switch-when="ms-date">
                                <div ms-date quest='quest'
                                 quest-num='{{(true)?numInc():return}}'>{{questNo}}</div>
                            </div>
                            <div ng-switch-when="ms-text">
                                <div ms-text quest='quest'
                                quest-num='{{(true)?numInc():return}}'>{{questNo}}</div>
                            </div>
                            <div ng-switch-when="ms-textarea">
                                <div ms-textarea quest='quest'
                                quest-num='{{(true)?numInc():return}}'>{{questNo}}</div>
                            </div>
                            <div ng-switch-when="ms-number">
                                <div ms-number quest='quest'
                                quest-num='{{(true)?numInc():return}}'>{{questNo}}</div>
                            </div>
                            <div ng-switch-when="ms-html">
                                <div ms-html quest='quest'></div>
                            </div>
                        </div>
                    </div>

What should be my true statement in the quest-num='{{(true)?numInc():return}}'>?

What i want to achieve is an increment a model value conditionaly when the statement is true, if it's true all the time the program breaks, what should be my true statement here?

numInc returns a ++ of a num value in the model, initialized first at 0, and when it hits the function it increments, but because i have ng-switch it increments too many times, that's why i need the true/false statement, i think...

Upvotes: 1

Views: 1153

Answers (2)

Mwayi
Mwayi

Reputation: 1623

I'm not sure I understood your question but if you wanted something like this

     Label    Actual $index value
Question 1    0
              1
Question 2    2
Question 3    3
         etc...

Then we can use a directive. Here's a quick sketch

var app = angular.module('app', []);

app.controller('MainController', function($scope){
    $scope.arr = [{name:'John', phone:'555-1276'},
     {name:'Mary', phone:'800-BIG-MARY'},
     {name:'Mike', phone:'555-4321', countMe:false},
     {name:'Adam', phone:'555-5678'},
     {name:'Julie', phone:'555-8765'},
     {name:'Juliette', phone:'555-5678', countMe:false}]
});

app.directive('conditionalNumberDirective', function(){
    var counter = 0;
    return {
      restrict: 'A',
      link: function(scope, el, attr) {
        // config what is item and what is coutMe via attrs here 
        if(scope.$index === 0) {
          counter = 1;
        }
        scope.counter = counter;
        if(angular.isDefined(scope.item) && angular.isDefined(scope.item.countMe) && !scope.item.countMe) {
          scope.counter = null;
        }else {
          counter++
        }
      }
    }
});

Html would look something like

<div ng-controller="MainController">
    <input type="text" ng-model="search"/>
    <div ng-repeat="item in arr | filter:search"  conditional-number-directive>
  Index:{{$index}} {{item}} - Label:{{counter}}
    </div>
</div>

Upvotes: 2

Venugopal Madathil
Venugopal Madathil

Reputation: 2021

Instead of $index use a ng-model variable. You could try something like this also(ternary):

 {{ true ? true : false }}

while assigning the value.

Upvotes: 0

Related Questions