如何跳过NG重复$指数 [英] How to skip $index in ng-repeat

查看:80
本文介绍了如何跳过NG重复$指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下几点:

   <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>

应该是什么我真实的陈述在追求-NUM ='{{(真正)numInc():返回}}?>

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

我想要实现的是一个增量的模型值conditionaly当说法是正确的,如果这是真的所有的时间程序中断,应该是什么我这里真实的陈述?

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返回模型中的一个NUM值的++,在0首先初始化,并且当它击中它增加的功能,但因为我有NG-开关它增加了太多次,这就是为什么我需要真正的/虚假陈述,我想......

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...

推荐答案

我不知道我理解你的问题,但如果你想这样的事情

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看起来像

<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>

这篇关于如何跳过NG重复$指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆