esvit / ng-table

Simple table with sorting and filtering on AngularJS
http://esvit.github.io/ng-table
BSD 3-Clause "New" or "Revised" License
2.77k stars 851 forks source link

'variable.reload() is not a function' when using tableParams as empty object #963

Open italopaiva opened 7 years ago

italopaiva commented 7 years ago

Not really sure if it is a bug or not, but it happened with me.

If you initialize a variable to receive an instance of NgTableParams as an empty object and then change its value asynchronously later, you get a TypeError saying that "newParams.reload is not a function". Despite of the table data is correctly shown (if you update the value correctly, of course), you will keep getting this error on console, which is unpleasant.

This is an example of the code:

angular.module('app').controller('Controller', Controller);

Controller.$inject = ['NgTableParams'];

function Controller(NgTableParams) {

    vm = this;

    vm.tableParams = {};

    // ...

    vm.init = function(){

        someArbitraryPromise.then(function(result){
            // ...

            vm.tableParams = new NgTableParams(/* ... whatever in here ... */);

            // ...
        });
    }

       // ...
}

This occurs because the following lines of code in ngTable.js (lines 1042 to 1048):

$scope.$watch('params', function(newParams, oldParams){
    if (newParams === oldParams || !newParams) {
        return;
    }

    newParams.reload();
}, false);

When newParams is an empty object, !newParams will not work and newParams.reload() will be called with newParams being an empty object. Some better validation whether newParams is empty or not is required to fix this.

I'm using ngTable v0.8.3 and Angular 1.5.5.