javascript - ng-Repeat( Angular )中的自动序列号

标签 javascript angularjs angularjs-ng-repeat

我正在使用 ng-Repeat 将 Controller (或来自服务)中的数据填充到表格中。但是,我需要自动在第一列中填写序列号。目前我在所有列中只获得静态数据 1。

HTML:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<table>
<tr><th>S.No.</th><th>Name</th></tr>
<tr ng-repeat="x in People"><td>1</td><td>{{x.name}}</td></tr>
</table>
<script>
var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
    $scope.People = [
    {name:"Peter"},
    {name:"Lina"},
    {name:"Roger"}
    ];
});
</script>
</body> 
</html>

输出:

enter image description here

我希望第一列中的数字为 1、2、3 等。如何使用 ngRepeat 实现此目的?

最佳答案

使用$index:

<td>{{$index + 1}}</td>
<td>{{x.name}}</td>

关于javascript - ng-Repeat( Angular )中的自动序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35057333/

相关文章:

javascript - AngularJS Controller 功能歧义

javascript - 通过 Promises 返回的 Firebase 多个异步请求

angularjs - Angular.js比较输入字段中的值(密码和密码确认)

javascript - ng-toggle,添加到数组,

javascript - 在 ng-repeat 之外访问和绑定(bind)数据

javascript - 错误 : Cannot find module "rxjs/operators" in Ionic Angular

javascript - 在标准 ES 6 类中添加 getter 方法有什么意义吗?

html - Css 和 AngularJS : How can I nest two classes in css selector if one class is generated with ng-class

javascript - Angular promise 无法正确解决

javascript - 在 ng-repeat 中获取图像尺寸并将其放入 ng-repeat item | AngularJS