我正在尝试在ng-repeat
模板中实现分页,我正在使用How to do paging in AngularJS?中找到的代码,即:http://jsfiddle.net/dalcib/J3fjc/。
我的代码与此jsfiddle示例中的代码几乎相同,除了不使用硬编码值,我使用一个注入服务,从返回json的url中获取数据。
我遇到的问题是jsfiddle代码中集合的长度总是为零,我不知道为什么因为我是新手。这是相关的代码:
...
collection = scope.$eval(rhs),
count = collection.length;
count
始终为零。看起来代码是在收到服务响应之前执行的。
任何人都可以帮忙吗?
答案 0 :(得分:1)
您不需要使用$ eval()
$ eval是取一个字符串并获得具有此名称的对象
您可以做的只是将您在服务或资源中获得的json对象的范围变量关联起来。
在ng-repeat中,您可以使用他的范围变量。
function gridCtrl($scope, sameResource) {
sameResource.query( function (todos) {
$scope.todos = todos;
}
}
在视图中
<div ng-repeat="todo in todos"> ...
答案 1 :(得分:0)
听起来像你正在寻找$ index。
例如:
<html ng-app>
<head></head>
<body ng-init="poops = ['fat tony','the turdis','hershey squirts']">
<div ng-repeat="poo in poops">{{ poo + "/" + $index}}</div>
</body>
</html>
输出:
fat tony/0
the turdis/1
hershey squirts/2