ng-table TypeError:无法设置属性' $ data'为null

时间:2014-04-06 10:41:28

标签: javascript angularjs ngtable

我正在使用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();
        });

    }

3 个答案:

答案 0 :(得分:6)

我知道这已经太晚了,但我想更好地为那些寻求同一问题答案的人发表评论。

我也有同样的问题,并通过以下网址中的示例设法修复。

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

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

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

答案 1 :(得分:1)

这是它开始为我工作的时候。我在params.settings中添加了范围。

controller: function() {
...
 $scope.myTableParams = new ngTableParams({
.
},{
...
getData: function ($defer, params) {
   if (params.settings().$scope == null) {
         params.settings().$scope = $scope;
   }
.
.
}

答案 2 :(得分:0)

here看看,设置 ngTableParams 的所有参数解决了问题