【问题标题】:ngHandsontable datarow undefined after asynchronous service callngHandsontable 数据行在异步服务调用后未定义
【发布时间】:2016-01-16 05:58:32
【问题描述】:

我有一个简单的控制器,它进行 ajax 调用以获取 ngHandsontable 的表数据,但 datarows 的值是 undefined

var app = angular.module('propnet', [
    'ngRoute',
    'ngHandsontable'
]);
// configure our routes
app.config(function($routeProvider)
{
    $routeProvider.    
    // route for the about page
    when('/property/:prop_id',
    {
        templateUrl: 'pages/property.html',
        controller: 'propertyController'
    })
...

app.controller('propertyController', function($scope, $routeParams, $http, $log)
{
    $http.get('api/Transactions/property/' + $routeParams.prop_id).success(function(data)
    {
        $scope.transactions = data;
        $log.log("Got some txns" + data);
    });

});

pages/property.html:

<div class="row">{{ transactions }}</div>
<div class='row'>
    <hot-table col-headers="showColHeaders" datarows="transactions">
        <hot-column data="itemName" title="'Name'" type="'text'" read-only></hot-column>
         <hot-column data="amount" title="'Amount'" type="'numeric'" format="'$ 0,0.00'" read-only></hot-column>
         <hot-column data="date" title="'Date'" type="'date'" dateFormat="'YYYY-MM-DD'"></hot-column>
         <hot-column data="vendorName" title="'Vendor'"></hot-column>
         <hot-column data="notes" title="'Notes'"  width="150" ></hot-column>
    </hot-table>
</div>

{{ transactions }} 打印从服务调用返回的值,但 &lt;hot-table&gt; 为空,并且 ngHandsontable 正在抛出错误(多次):

Got some txns[object Object],[object Object],[object Object],[object Object]

angular.js:12477 TypeError: Cannot read property 'length' of undefined
    at Object.fn (ngHandsontable.js:253)
    at Scope.$digest (angular.js:15826)
    at Scope.$apply (angular.js:16097)
    at done (angular.js:10546)
    at completeRequest (angular.js:10744)
    at XMLHttpRequest.requestLoaded (angular.js:10685)(anonymous function) @ angular.js:12477(anonymous function) @ angular.js:9246Scope.$digest @ angular.js:15844Scope.$apply @ angular.js:16097done @ angular.js:10546completeRequest @ angular.js:10744requestLoaded @ angular.js:10685

为什么$scope.transactions 不绑定到datarows

相关:ngHandsontable shows no content on rest service call

【问题讨论】:

标签: javascript angularjs handsontable


【解决方案1】:

在你的控制器中初始化 $scope.transactions = []。最初,当handsontable 尝试绑定数据时,$scope.transactions 是未定义的。因此,首先初始化它有助于传递错误。一旦 GET 方法返回数据,handsontable 就会用数据填充表格。

【讨论】:

  • Initializing $scope.transactions 修复了错误,但表中仍然没有数据。原来我必须(?)在控制器中指定 minSpareRows 以满足第 253 行条件:if (oldValue.length == scope.htSettings.minSpareRows &amp;&amp; newValue.length != scope.htSettings.minSpareRows) { scope.htSettings['data'] = scope.datarows;
猜你喜欢
  • 2018-08-09
  • 2016-10-14
  • 2017-10-05
  • 2017-01-08
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
相关资源
最近更新 更多