gpt4 book ai didi

javascript - ng 表类型错误 : Cannot set property '$data' of null

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:45 26 4
gpt4 key购买 nike

我正在使用 ng-table 显示来自 API 调用的信息,我将其分组,就像 website 上的示例一样.

但是,网站上的示例使用了静态信息,因为我每次都需要进行新的 API 调用。当我不包含 $scope.$watch('groupby', function(value) 函数时,我可以在第一次初始调用时显示表格。

angular.module('myApp.controllers', ['ngTable','ui.bootstrap','dialogs'])
.controller('HomeCtrl', function($scope,$log, $rootScope, $modal, TimeService, EventServiceAPI, $filter, ngTableParams) {
var datum = new Date().setHours(0,0,0,0);
$scope.datum = datum;
var timestamp = Math.round(new Date().getTime()/1000);


var findAllEvents = function(timestampdata) {
EventServiceAPI.query({start: timestampdata, end: timestampdata + 3*24*60*60}).$promise.then(function(data) {

//var data = $filter('orderBy')(data, 'event.startHour');

$scope.$watch("data", function () {
$scope.tableParams.reload();
});

$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 100 // count per page

}, {
groupBy: function(item) {
return 'Start vanaf ' + item.event.startHour + ' uur';
},
total: data.length,
getData: function($defer, params) {
var orderedData = params.sorting() ?
$filter('orderBy')(data, 'event.startHour') :
data;

$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});

$scope.$watch('groupby', function(value){
$scope.tableParams.settings().groupBy = value;
console.log('Scope Value', $scope.groupby);
console.log('Watch value', this.last);
console.log('new table',$scope.tableParams);
$scope.tableParams.reload();
});
})
};

findAllEvents(timestamp);

$scope.getEarlier = function() {
datum -= (24 * 60 * 60 * 1000);
$scope.datum = datum;
var timestamp = Math.round(datum/1000);
console.log(timestamp);
findAllEvents(timestamp);

};
});

编辑:

我现在可以显示数据,但是没有显示组:

var datum = new Date().setHours(0,0,0,0);
$scope.datum = datum;
var timestamp = Math.round(new Date().getTime()/1000);

EventServiceAPI.query({start: timestamp, end: timestamp + 3*24*60*60}).$promise.then(function(data) { $scope.data = data; initlizeTable(); });


var initlizeTable = function() {
// Table
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 100 // count per page

}, {
groupBy: function(item) {
return 'Start vanaf ' + $scope.data.event.startHour + ' uur';
},
total: $scope.data.length,
getData: function($defer, params) {
var data = EventServiceAPI.query({start: timestamp, end: timestamp + 3*24*60*60});
var orderedData = params.sorting() ?
$filter('orderBy')($scope.data, 'event.startHour') :
$scope.data;

$defer.resolve($scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}, $scope: { $data: $scope.data }
});

$scope.$watch('groupby', function(value){
$scope.tableParams.settings().groupBy = value;
console.log('Scope Value', $scope.groupby);
console.log('Watch value', this.last);
console.log('new table',$scope.tableParams);
$scope.tableParams.reload();
});

$scope.$watch('data', function(value){
$scope.tableParams.settings().groupBy = value;
console.log('Scope Value', $scope.groupby);
console.log('Watch value', this.last);
console.log('new table',$scope.tableParams);
$scope.tableParams.reload();
});

}

最佳答案

我知道这为时已晚,但我认为最好对寻求同一问题答案的其他人对此发表评论。

我也遇到了同样的问题,并按照以下 URL 中的示例设法解决了。

https://github.com/esvit/ng-table/blob/master/src/scripts/04-controller.js

我刚刚在“ngTableParams”实例创建后将以下代码行添加到我的代码中。

$scope.params.settings().$scope = $scope;

关于javascript - ng 表类型错误 : Cannot set property '$data' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22892908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com