2

I am trying to use ng-table but stuck with ngTableParams. I keep getting the

[Error] Error: 'undefined' is not a constructor (evaluating 'new ngTableParams')

error, no matter what things I try.

The current code looks like

$scope.tableParams = new ngTableParams({
    page: 1,
    count: 200,
    sorting: {
        name: 'asc'
    }
}, {
    groupBy: 'area',
    total: TheData.length,
    getData: function($defer, params) {
        // use build-in angular filter
        var orderedData = params.sorting() ? $filter('orderBy')(TheData, params.orderBy()) : TheData;
        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }
});

The app and controller are invoked with ngTable and ngTableParams:

angular.module('MegaList', ['ui.bootstrap', 'ngTable']);
angular.module('MegaList').controller('DisplayMegaList', ['$scope', 'ngTableParams', function($scope, $http, ngTableParams) {
    ...
}

I think I've already tried all the ways to compose 'ngTable', 'ngTableParams' and ngTableParams keywords together, but it still just doesn't work.

What should I try then?

3 Answers 3

3

This is because you are missing parameter. All have to matched as well

angular.module('MegaList')
       .controller('DisplayMegaList', ['$scope', '$http', 'ngTableParams', function($scope, $http, ngTableParams)
1
  • 2
    In fact, the order of dependencies matters. It must match the arguments. i.e. ['$scope', 'ngTableParams', function($scope, ngTableParams, $http) {}] May 23, 2015 at 7:03
-1

It looks like you create controller on a different module. Try to use MegaList.controller(...) instead of FloristList.controller(...).

2
  • It's not the case, I just mistyped the code. The module is all the same. In description section of this question, I rewrote code a little to avoid any unnecessary variables, so now it's clear that everything happens within one module.
    – rishat
    Oct 1, 2014 at 12:44
  • It looks like now you have unequal number of parameters. try: angular.module('MegaList').controller('DisplayMegaList', ['$scope', '$http', 'ngTableParams', function($scope, $http, ngTableParams)
    – Kamil R
    Oct 1, 2014 at 12:53
-1

I had a similar problem and just copying the beforeEach from this sample helped me: https://docs.angularjs.org/guide/unit-testing

beforeEach(inject(function(_$controller_){
  // The injector unwraps the underscores (_) from around the parameter names when matching
  $controller = _$controller_;
}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.