4

请帮忙。我被困在这一点上超过 14 个小时,我找不到解释或例子。

这就是我现在拥有的:http: //plnkr.co/edit/PaYR7c0QXSKxl1jcmRBQ

如果我不使用角度路由,ngTable 可以正常工作。一旦我添加路由,它就会出现此错误。当我评论 $scope.tableParams = new ngTableParams({ ...但随后数据未显示时,错误消失了。

不起作用的代码:

    var app = angular.module('main', [
  'ngRoute',
  'ngTable'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/', {templateUrl: 'partial1.html', controller: 'MyCtrl1'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

app.controller('MyCtrl1', [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}];

    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10           // count per page
    }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });   
  }]);
    </script>
  </head>

  <body ng-app="main">
    <div ng-view=""></div>
  </body>
4

1 回答 1

1

应更正 HTML 标头:(您在 Angular 之前加载 ngTable)

<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@*" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular.js"></script>
<script data-require="angular-route@*" data-semver="1.2.10" src="http://code.angularjs.org/1.2.10/angular-route.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>

控制器声明有点偏离。

app.controller('MyCtrl1', ['$scope', 'ngTableParams', function MyCtrl1($scope, ngTableParams) {
于 2014-05-19T02:54:56.783 回答