Angular CSS ngRepeat与li之间的分隔符

时间:2016-12-14 09:06:29

标签: javascript html css angularjs ng-repeat

我想制作一个简单的自定义分页,但我不知道如何在两次ngRepeat迭代之间进行分隔。 目前分隔符位于末尾,而不是介于两者之间。



function Controller($scope)
{
    $scope.currentPage = 1;
    $scope.totalPages = 2;
    $scope.itemPerPage = 12;

    $scope.getPages = function ()
    {
      return new Array($scope.totalPages);
    };

    $scope.pageChange = function (page)
    {
        if(isNaN(page))
        {
            if((page == "-" && 1 < $scope.currentPage) || (page == "+" && $scope.currentPage < $scope.totalPages))
            {
                $scope.currentPage = eval($scope.currentPage + page + 1);
            }
        }
        else
        {
            $scope.currentPage = page;
        }
    };
}
&#13;
ul
{
  list-style: none;
  display: inline-block;
  vertical-align: middle;
  margin-bottom: 0;
}

li
{
  display: inline-block;
  vertical-align: middle;
  margin: 0 -1px;
}

a.active
{
  background: #31a445;
  color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-controller="Controller">
  <ul>
      <li ng-repeat="page in getPages() track by $index">
          <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a>
      </li>
      <li ng-hide="$last" class="paging-spacer"><span>-</span></li>
  </ul>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

您应该使用ng-repeat-startng-repeat-end来创建重复的块

<li ng-repeat-start="page in getPages() track by $index">
   <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a>
</li>
<li ng-hide="$last" ng-repeat-end class="paging-spacer"><span>-</span></li>

答案 1 :(得分:1)

您的separator位于ng-repeat之外 - 使用ng-repeat-startng-repeat-end,如下所示:

&#13;
&#13;
function Controller($scope) {
  $scope.currentPage = 1;
  $scope.totalPages = 2;
  $scope.itemPerPage = 12;

  $scope.getPages = function() {
    return new Array($scope.totalPages);
  };

  $scope.pageChange = function(page) {
    if (isNaN(page)) {
      if ((page == "-" && 1 < $scope.currentPage) || (page == "+" && $scope.currentPage < $scope.totalPages)) {
        $scope.currentPage = eval($scope.currentPage + page + 1);

        getProducts();
      }
    } else {
      $scope.currentPage = page;

      getProducts();
    }
  };
}
&#13;
ul {
  list-style: none;
  display: inline-block;
  vertical-align: middle;
  margin-bottom: 0;
}
li {
  display: inline-block;
  vertical-align: middle;
  margin: 0 -1px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-controller="Controller">
  <ul>
    <li ng-repeat-start="page in getPages() track by $index">
      <a href="" ng-click="pageChange($index + 1)" ng-class="{active: currentPage == $index + 1}">{{$index + 1}}</a>
    </li>
    <li ng-hide="$last" class="paging-spacer"><span>-</span>
    </li>
    <li ng-repeat-end></li>
  </ul>
</div>
&#13;
&#13;
&#13;