在ng-repeat中显示作用域值变化时的下一系列数据



我试过了

startIndex = 0;
lengthIndex = 5;
<span aria-hidden="true" class="glyphicon glyphicon-chevron-left" ng-click="startIndex = startIndex-1"></span>
  <ul>
<li ng-repeat="week in mydata | limitTo:lengthIndex:startIndex">
 </ul>
<span aria-hidden="true" class="glyphicon glyphicon-chevron-left" ng-click="startIndex = startIndex-1"></span>

当作用域值改变时,我希望显示下一组数据。但是当页面刷新时,它会正确显示数据,即数据片。但点击范围值在变化,但数据不变。

ng-repeat创建一个新的作用域,因此当您尝试访问startIndex时,它将访问子作用域并仅更新子作用域中的值。要更改ng-repeat的父作用域中的值,可以使用$parent.startIndex,如下所示

<li ng-repeat="week in mydata | limitTo:lengthIndex:startIndex">
<span aria-hidden="true" class="glyphicon glyphicon-chevron-left" ng-click="$parent.startIndex = $parent.startIndex-1"></span>

或使startIndex成为一个对象,以确保访问和更新父属性。

<li ng-repeat="week in mydata | limitTo:lengthIndex:index.startIndex">
<span aria-hidden="true" class="glyphicon glyphicon-chevron-left" ng-click="index.startIndex = index.startIndex-1"></span>

这是一个工作的例子,根据您的更新代码

当你使用ng-repeat时,angular的摘要过程会一直持续到循环结束。因此,它将从作用域断开到索引的链接,并在刷新后进行反射,因为它当时提供了新的作用域。

这只是一个传统的异步函数,在最后调用$scope.$apply(),告诉AngularJS一个异步事件刚刚发生了。

所以当你改变了ng-repeat中的作用域变量时,必须使用$scope.$apply().

最新更新