【问题标题】:ng-table TypeError: undefined is not a functionng-table TypeError: undefined is not a function
【发布时间】:2016-11-23 03:15:24
【问题描述】:

我使用的是旧版本的 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-resource 有效

所以使用 Restangular 进行查询工作

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

也许在 ng-table 0.4.3 及更高版本中我不能再使用角度资源?或者我只是做错了。这是我的发票资源(在 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)

【问题讨论】:

    标签: angularjs coffeescript ngtable


    【解决方案1】:

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

    提供给 getData 方法的 $defer 参数已被删除。 相反,您的 getData 方法应该返回一个数组或一个承诺 解析为数组。

    getData: ($defer, params) ->
    

    由于该更改,您的 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
    

    【讨论】:

    • 我已经尝试了您的建议,但仍然遇到同样的错误。 ng-table 网站上的迁移是针对 1.0.0 版本的,而不是像 0.4.3 这样的旧版本。
    • 现在删除 $defer 后,当我查看 params.url() 时,它也不存在了。所以我不得不把 $defer 放回去。
    【解决方案2】:

    我发现 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
      )
    

    【讨论】:

      【解决方案3】:

      先初始化,成功了!

      .controller('indexDataCtrl', function($scope, $q, NgTableParams) {
      
      $scope.dataTable = new NgTableParams();
      
      ....
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-24
        • 2021-06-21
        • 1970-01-01
        • 2014-08-15
        • 2015-01-14
        • 2014-07-06
        • 2014-09-01
        • 2020-02-15
        • 2019-10-14
        相关资源
        最近更新 更多