ng-table TypeError: undefined is not a function

ng-table TypeError: undefined is not a function

我使用的是旧版本的 ng-tables 0.4.2,如果我升级到 0.4.3,它会在我的 chromium 控制台中显示错误

TypeError: undefined is not a function
    at reload (http://localhost:3000/assets/ng-table/ng-table.min.js?body=1:140:160)
    at Object.fn (http://localhost:3000/assets/ng-table/ng-table.min.js?body=1:196:24)
    at Scope.$digest (http://localhost:3000/assets/angular/angular.js?body=1:5226:31)
    at Scope.$apply (http://localhost:3000/assets/angular/angular.js?body=1:5305:28)
    at done (http://localhost:3000/assets/angular/angular.js?body=1:3182:26)
    at completeRequest (http://localhost:3000/assets/angular/angular.js?body=1:3306:9)
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:3000/assets/angular/angular.js?body=1:3279:13) 

这是我配置 tableParams

的方式
  page = $location.search()['page'] # Get page from the URL

  # data is created by ngTableParams
  $scope.tableParams = new ngTableParams({
    page: if page then page else 1,
    count: 10,
    sorting: { invoice_no: 'desc'}
  }, {
    total: 0,
    filterDelay: 500,

    # ng-table will ask for updated data, this is where it gets it from.
    getData: ($defer, params) ->
      # Go get a list of debtors
      Invoice.query params.url(), (data) ->
        params.total($scope.total)
        $location.search(params.url()) # Params into URL
        orderedData = (if params.sorting then $filter('orderBy')(data, params.orderBy()) else data) # Sort
        $defer.resolve(orderedData) # Paginate / update table with new data
  })

我在我的模块中包含 ng-tables,在我的控制器中包含 ngTableParams。

更新:传递模拟数据有效

如果我执行以下操作没有错误。

# ng-table will ask for updated data, this is where it gets it from.
getData: ($defer, params) ->
  $defer.resolve([{invoice_no: 1}])

更新:使用 Restangular 代替 angular-资源有效

所以使用 Restangular 进行查询是可行的

getData: ($defer, params) ->
  Restangular.all('invoices').getList()
  .then((data) ->
    return data
  )    

也许在 ng-table 0.4.3 及更高版本中我不能再使用 angular-资源?或者我只是做错了。这是我的发票资源(在 ng-table 0.4.2 中工作)。

Invoice = ['$resource', 'apiPrefix', ($resource, apiPrefix) ->
  $resource( apiPrefix + "/invoices/:id",
    id: "@id"
  ,
    # Add update method
    update: {method: "PUT"}
  )
]

angular
  .module('paisApp')
  .factory('Invoice', Invoice)

我在这里找到了答案:https://github.com/esvit/ng-table 在 3 号。

The $defer paramater supplied to your getData method has been removed. Instead your getData method should return an array or a promise that resolves to an array.

getData: ($defer, params) ->

由于该更改,您的参数 var 未定义。

要迁移您应该工作的函数:

getData: (params) ->
  # Go get a list of debtors
  Invoice.query params.url(), (data) ->
    params.total($scope.total)
    $location.search(params.url()) # Params into URL
    orderedData = (if params.sorting then $filter('orderBy')(data, params.orderBy()) else data) # Sort
    orderedData

我发现 Invoice 资源正在返回 ng-tables 不喜欢的类型。 ng-tables 版本 0.4.3 期望返回 $defer 对象,因此我不得不将查询包装在 defer.resolve 中。现在可以了。

  $defer.resolve(
    Invoice.query params.url(), (data) ->
      orderedData = (if params.sorting then $filter('orderBy')(data, params.orderBy()) else data) # Sort
      return orderedData
  )

先初始化,成功了!

.controller('indexDataCtrl', function($scope, $q, NgTableParams) {

$scope.dataTable = new NgTableParams();

....

}