Javascript Angularjs ng-repeat 列表的自动编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29091830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:50:30  来源:igfitidea点击:

Auto numbering of Angularjs ng-repeat list

javascriptangularjs

提问by David Addoteye

I am creating student report list with Angularjs ng-repeat. My issue if how i can dynamically appending numbering like ordered-list to the generated list in view. I want to achieve something like this

我正在使用 Angularjs ng-repeat 创建学生报告列表。我的问题是如何将有序列表等编号动态附加到视图中生成的列表中。我想实现这样的目标

 # | Name of student | Student ID
 _________________________________
 1 | Samuel Addo     | 346578
 2 | GRace Asumani   | 965433
 3 | Zein Akill      | 123455
 4 | David Addoteye  | 678543

The '#' column should be auto generate when rendering the model in view through ng-repeat. Honestly I don't know where to start cos i don't know how to do it. I will be glad if anyone can help me or point me to the right source. Thank you.

通过 ng-repeat 在视图中渲染模型时,“#”列应该是自动生成的。老实说,我不知道从哪里开始,因为我不知道该怎么做。如果有人可以帮助我或为我指出正确的来源,我会很高兴。谢谢你。

回答by juunas

Inside ng-repeat, you can use:

在 ng-repeat 中,您可以使用:

{{$index +1}}

So for example:

例如:

<tr ng-repeat="student in students">
  <td>#{{$index + 1}}</td>
  <td>{{student.name}}</td>
</tr>

$index is a variable provided by the ng-repeat directive which gives you the current index. Here I added 1 to it so that the numbers start from 1 instead of 0. ng-repeat documentation

$index 是 ng-repeat 指令提供的一个变量,它为您提供当前索引。在这里我给它加了 1,这样数字从 1 而不是 0 开始。ng-repeat 文档

回答by TechnoCrat

Yes you can use {{$index}} to print index position for serial no inside ng-repeat.

是的,您可以使用 {{$index}} 在 ng-repeat 中打印序列号的索引位置。

Various other variables also available for calculation and check for first, middle, last, odd, even using $first, $middle, $last, $odd and $even respectively.

各种其他变量也可用于计算和检查 first、middle、last、odd、even 分别使用 $first、$middle、$last、$odd 和 $even。

  • $index: iterator offset of the repeated element (0..length-1)
  • $first: true if the repeated element is first in the iterator.
  • $middle: true if the repeated element is between the first and last in the iterator.
  • $last: true if the repeated element is last in the iterator.
  • $even: true if the iterator position $index is even (otherwise false).
  • $odd: true if the iterator position $index is odd (otherwise false).
  • $ index: 重复元素的迭代器偏移量(0..length-1)
  • $ first: 如果重复元素在迭代器中是第一个,则为真。
  • $ middle: 如果重复元素在迭代器中的第一个和最后一个之间,则为真。
  • $ last: 如果重复元素在迭代器中是最后一个,则为真。
  • $ even:如果迭代器位置 $index 是偶数(否则为假),则为真。
  • $ odd:如果迭代器位置 $index 为奇数则为真(否则为假)。