0

首先我使用了一个 ng table pager 模板

我无法使用我的自定义函数,例如 ng-click='first()' 而不是 ng-click="params.page(page.number)"

然后在其范围内调用参数。

任何人都可以帮我解决这个问题。谢谢。

4

1 回答 1

1

绑定到 ngTable 的自定义分页模板创建了一个新范围,因为只有使用 params.page(1) 等参数定义的函数才能工作。因此,可以使用一组单独的控件来提供分页功能,而不是将自定义分页模板绑定到 ngTable。

查看 plunkr:http ://plnkr.co/edit/dZf4UkRbhQd2Jixi9f39?p=preview

索引.html

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>
    <script data-require="ng-table@*" data-semver="0.3.0" src="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.js"></script>

    <link data-require="ng-table@*" data-semver="0.3.0" rel="stylesheet" href="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.css" />
    <link data-require="bootstrap-css@*" data-semver="3.0.0" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

<body ng-app="main" ng-controller="DemoCtrl">

    <table ng-table="tableParams" class="table">
        <tr ng-repeat="user in $data">
            <td data-title="'Name'">
                {{user.name}}
            </td>
            <td data-title="'Age'">
                {{user.age}}
            </td>
        </tr>
    </table>

    <p><strong>Page:</strong> {{ tableParams.page() }}</p>
    <button class="btn btn-default" ng-click="previous()">Previous</button>
    <button class="btn btn-default" ng-click="next()">Next</button>
    <button class="btn btn-default" ng-click="info()">Info</button>

</body>
</html>

脚本.js

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, ngTableParams) {
    var data = [{name: "Moroni", age: 50},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}];

    var count=1;            



    $scope.tableParams = new ngTableParams({
        page: 1,   // show first page
        count: 5 , // count per page

    }, {
        counts: [], // hide page counts control
        total: 1,  // value less than count hide pagination
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));

          this.current=params.count();
        }

    });

    $scope.next=function(){
      console.log("next called");
    count++;  
     $scope.tableParams.page(count);
   };

   $scope.previous=function(){
     console.log("previous called");
     if(count>1)
      count--;  
     $scope.tableParams.page(count);

   }




   $scope.info=function(){
     console.log("info called");
     console.log("page: "+(($scope.tableParams.page() - 1) * $scope.tableParams.count())+"to"+($scope.tableParams.page() * $scope.tableParams.count()));
   }

});

在这里,我在控制器中定义了自定义函数并使用 $scope.tableParams.page(count) 来分页到特定页面。

于 2014-08-06T10:09:22.177 回答