3

我在 Rails 项目中使用 AngularJS 1.2.10 稳定版,我试图让 ng-table 处理我的数据,但没有显示任何内容。

我已经包含了 ng-table 文件并将其包含在我的角度模块中。当我刷新页面时,我正在获取数据,但是如果我离开这个视图,然后在单页应用程序中再次返回,它不会加载数据。即:刷新列表上的浏览器并且所有数据都在那里,我去查看一个单独的项目,我被带到一个新页面(通过 angularJS 路由)然后我返回到列表(再次通过 angularJS 路由)和数据为空/没有列表。

这是我的桌子

<table ng-table="tableParams" class="table">
  <tr ng-repeat="debtor in debtors">
    <td data-title="'Code'" sortable="'code'">{{debtor.code}}</td>
    <td data-title="'Name'" sortable="'name'">{{debtor.name}}</td>
    <td>
      <a href="/api#/clients/{{debtor.id}}">Show</a>
      <a href="/api#/clients/{{debtor.id}}/edit">Edit</a>
      <a href="" ng-confirm-click="destroy(debtor.id)">Delete</a>
    </td>
  </tr>
</table>

这是我获取数据的控制器。

app.controller "DebtorsController", ['$window', '$scope', '$http', '$location', '$state', '$stateParams', '$modal', '$timeout', 'flash', 'Common', 'Debtor', 'Client', 'ngTableParams', '$filter', ($window, $scope, $http, $location, $state, $stateParams, $modal, $timeout, flash, Common, Debtor, Client, ngTableParams, $filter) ->

  # --------------------------------------------------------------------------------
  # Initialise
  # --------------------------------------------------------------------------------
  $scope.debtors = {}

  # --------------------------------------------------------------------------------
  # Index
  # --------------------------------------------------------------------------------
  if $state.current.name == "debtors"
    Debtor.query {},
      (data) ->
        # Define ng-table params
        $scope.tableParams = new ngTableParams({
          page: 1,
          total: data.length,
          count: 10
        })

  # --------------------------------------------------------------------------------
  # Index
  # --------------------------------------------------------------------------------
  if $state.current.name == "debtors"
    $data = []
    Debtor.query {}, (response) -> $data = response
    console.log "debtors index"

    $scope.tableParams = new ngTableParams({
      page: 1,
      count: 10,
      sorting: { name: 'asc'}
    }, {
      total: $data.length,
      getData: ($defer, params) ->
        console.log "run deferred"
        orderedData = (if params.sorting then $filter('orderBy')($data, params.orderBy()) else $data)
        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
    })

我可以在 chromes 控制台中看到它正在调用

XHR finished loading: "http://localhost:3000/assets/layouts/default.html.erb". angular-min.js?body=1:1825
XHR finished loading: "http://localhost:3000/assets/debtors/index.html.erb". angular-min.js?body=1:1825

debtors index  

ngTable: set settings 
Object {$scope: null, $loading: false, data: null, total: 0, counts: Array[4]…}
 angular-min.js?body=1:2147
ngTable: set parameters 
Object {page: 1, count: 10, filter: Object, sorting: Object, group: Object…}
 angular-min.js?body=1:2147
XHR finished loading: "http://localhost:3000/debtors". angular-min.js?body=1:1825
XHR finished loading: "http://localhost:3000/assets/debtors/_listing.html.erb". angular-min.js?body=1:1825
ngTable: reload data angular-min.js?body=1:2147
ngTable: current scope 
$get.h.$new.a {$id: "006", this: $get.h.$new.a, $$listeners: Object, $$listenerCount: Object, $parent: $get.h.$new.a…}
 angular-min.js?body=1:2147

run deferred

ngTable: reload data angular-min.js?body=1:2147
ngTable: current scope 
$get.h.$new.a {$id: "006", this: $get.h.$new.a, $$listeners: Object, $$listenerCount: Object, $parent: $get.h.$new.a…}
 angular-min.js?body=1:2147

(I clicked on 'show' for one of the items)

XHR finished loading: "http://localhost:3000/assets/clients/show.html.erb". angular-min.js?body=1:1825
XHR finished loading: "http://localhost:3000/clients/2". angular-min.js?body=1:1825
XHR finished loading: "http://localhost:3000/locations.json". angular-min.js?body=1:1825

(I clicked on 'listing' which takes me back to the debtors listing)

debtors index

ngTable: set settings 
Object {$scope: null, $loading: false, data: null, total: 0, counts: Array[4]…}
 angular-min.js?body=1:2147
ngTable: set parameters 
Object {page: 1, count: 10, filter: Object, sorting: Object, group: Object…}
 angular-min.js?body=1:2147
ngTable: reload data angular-min.js?body=1:2147
ngTable: current scope 
$get.h.$new.a {$id: "017", this: $get.h.$new.a, $$listeners: Object, $$listenerCount: Object, $parent: $get.h.$new.a…}
 angular-min.js?body=1:2147

run deferred

ngTable: reload data angular-min.js?body=1:2147
ngTable: current scope 
$get.h.$new.a {$id: "017", this: $get.h.$new.a, $$listeners: Object, $$listenerCount: Object, $parent: $get.h.$new.a…}
 angular-min.js?body=1:2147
XHR finished loading: "http://localhost:3000/debtors". 

在返回债务人清单时,它没有调用这两行:

XHR finished loading: "http://localhost:3000/debtors". angular-min.js?body=1:1825
XHR finished loading: "http://localhost:3000/assets/debtors/_listing.html.erb". angular-

我认为它必须重新加载数据,而不是模板,尽管我可以看到。

4

1 回答 1

2

注意到我必须使用 ngTableParams 的 getData 部分获取数据,并等待它与我的数据一起成功返回,然后再应用过滤器并将其返回以进行渲染。

  # --------------------------------------------------------------------------------
  # Index
  # --------------------------------------------------------------------------------
  if $state.current.name == "debtors"

    # Setup ng-table with defaults and data
    $scope.tableParams = new ngTableParams({
      page: 1,
      count: 10,
      sorting: { name: 'asc'}
    }, {
      total: $data.length,
      # ng-table will ask for updated data, this is where it gets it from.
      getData: ($defer, params) ->
        Debtor.query {}, (response) ->
          $data = response
          console.log 'run deferred'
          orderedData = (if params.sorting then $filter('orderBy')($data, params.orderBy()) else $data)
          $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
    })
于 2014-02-12T04:01:21.113 回答