简体   繁体   English

如何增加角度模板内的 $index 值?

[英]How to increase the $index value inside the angular template?

I want to display my data in table rows, with 2 element per row.我想在表格行中显示我的数据,每行 2 个元素。

For example, if I have this array:例如,如果我有这个数组:

['dinesh', 'kumar', 'mahesh', 'rathore']

The result should look like:结果应如下所示:

--------------------
| dinesh | kumar   |
--------------------
| mahesj | rathore |
--------------------

When I put the ng-repeat in the tr , each row has one cell.当我将ng-repeat放入tr ,每一行都有一个单元格。

When I put the ng-repeat in the td , I am getting a single row with 4 cells当我将ng-repeat放入td ,我得到一行有 4 个单元格

I tried using $index + 1 but it did not change the actual index value.我尝试使用$index + 1但它没有改变实际的索引值。 So I am getting repeated elements.所以我得到了重复的元素。 What's the solution?解决办法是什么?

$index is the current repeat iteration. $index 是当前的重复迭代。 You cannot change the index value, you can only use it for display purpose.您不能更改索引值,只能将其用于显示目的。


There is a similar post on stackoverflow stackoverflow上也有类似的帖子

A solution would be to repeat over a splitted model:一种解决方案是在拆分模型上重复:

function chunk(arr, size) {
  var newArr = [];
  for (var i=0; i<arr.length; i+=size) {
    newArr.push(arr.slice(i, i+size));
  }
  return newArr;
}

$scope.chunkedData = chunk(['dinesh', 'kumar', 'mahesh', 'rathore'], 3);

Then:然后:

<table>
  <tr ng-repeat="row in chunkedData">
    <td ng-repeat="element in row">
      {{element}}
    </td>
  </tr>
</table>

(Follow the link if you need to order items verticaly) (如果您需要垂直订购商品,请点击链接)


But using table tr/td is a full pain.但是使用表 tr/td 是一个痛苦的过程。 Using div with css is probably a better solution.将 div 与 css 一起使用可能是更好的解决方案。 It would look like它看起来像

<div ng-repeat="element in elements" ng-style="{'float:left;' : $even}">
      {{element}}
</div 

add this to your javascript code..将此添加到您的 javascript 代码中..

    // divide array
Array.prototype.divideIt = function(d){
    if(this.length <= d) return this;
    var arr = this,
        hold = [],
        ref = -1;
    for(var i = 0; i < arr.length; i++){
        if(i % d === 0){
            ref++;
        }
        if(typeof hold[ref] === 'undefined'){
            hold[ref] = [];
        }
        hold[ref].push(arr[i]);
    }

    return hold;
};

Then use the following angularjs solution (similar to jbigman solution):然后使用以下 angularjs 解决方案(类似于 jbigman 解决方案):

<div ng-app>
    <table>
        <tr ng-repeat="subModel in ['dinesh', 'kumar', 'mahesh', 'rathore'].divideIt(2)">
            <td ng-repeat="element in subModel ">{{element}}</td>
        </tr>
    </table>
</div>

 // divide array Array.prototype.divideIt = function(d) { if (this.length <= d) return this; var arr = this, hold = [], ref = -1; for (var i = 0; i < arr.length; i++) { if (i % d === 0) { ref++; } if (typeof hold[ref] === 'undefined') { hold[ref] = []; } hold[ref].push(arr[i]); } return hold; };
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app> <table> <tr ng-repeat="subModel in ['dinesh', 'kumar', 'mahesh', 'rathore'].divideIt(2)"> <td ng-repeat="element in subModel ">{{element}}</td> </tr> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM