如何查找最后一个li是否在角度js中有效

时间:2016-07-14 13:05:18

标签: angularjs

我正在使用ng-repeat重复li,我需要找到最后一个li激活,如果最后一个激活,我需要禁用prev按钮。

<div class="pagination">
    <button ng-disabled="start == 0" ng-click="prevPage()">Prev</button>
    <ul>
      <li ng-repeat="i in getNumber(myNumber)  | limitTo: pagerStep track by $index" ng-class="{active: $index == selected}" ng-click="select($index)">
        <button ng-click="loadFromMenu($index+1)" ng-class="pageList">
          {{$index+1}}
        </button>
      </li>
    </ul>
    <button ng-disabled="start == lastStart" ng-click="nextPage()">Next</button>
  </div>

1 个答案:

答案 0 :(得分:2)

select()函数添加另一个参数:isLast,并从视图ng-click="select($index, $last)"调用它然后你可以引发另一个标志,指示最后一次点击的元素是否是最后一个ngRepeat并使用该标志禁用prev按钮:

查看:

<div class="pagination">
    <button ng-disabled="start == 0" ng-click="prevPage()">Prev</button>
    <ul>
      <li ng-repeat="i in getNumber(myNumber)  | limitTo: pagerStep track by $index" ng-class="{active: $index == selected}" ng-click="select($index, $last)">
        <button ng-click="loadFromMenu($index+1)" ng-class="pageList">
          {{$index+1}}
        </button>
      </li>
    </ul>
    <button ng-disabled="isLast" ng-click="nextPage()">Next</button>
  </div>

控制器:

$scope.select = function(selectedIndex, isLast) {
   $scope.selected = selectedIndex;
   $scope.isLast = isLast;
   // And the rest of your code of course 
}