在AngularJS中,可以使用$filter服务中的orderBy过滤器来保持$index与数据列表中的顺序一致。
以下是一个示例代码:
HTML模板:
{{$index+1}}. {{item.name}}
控制器:
app.controller('MyController', function($scope, $filter) {
$scope.items = [
{id: 3, name: 'Item 3'},
{id: 1, name: 'Item 1'},
{id: 2, name: 'Item 2'}
];
// 对数据列表进行排序,按照id字段升序排列
$scope.items = $filter('orderBy')($scope.items, 'id');
});
在上面的示例中,ng-repeat指令用于循环遍历数据列表中的每个项目。使用orderBy过滤器对数据列表进行排序,按照id字段升序排列。这样,$index就与数据列表中的顺序一致。
请注意,orderBy过滤器返回一个新的排序后的数组,因此需要将其赋值给$scope.items,以便在模板中使用。
这样,无论在哪一页,$index都会与排序后的数据列表中的顺序一致。