user2981029
user2981029

Reputation: 584

Error ngTable AngularJS

I'm using ngTable (AngularJS) to display a list of elements, but I get an error when compiling:

************ I am OK ******************

articles.client.controller.js:12 ReferenceError: articles is not defined at new (...../modules/articles/controllers articles.client.controller.js:27:16)

This is my code: myApp/public/modules/articles/controllers articles.client.controller.js

'use strict';

angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 
    'Authentication', 'Articles', 
    function($scope, $filter, ngTableParams, $stateParams, $location, Authentication, Articles){

        $scope.authentication = Authentication;
        $scope.find = function() {
            $scope.articles = Articles.query();
        };

        console.log('************ I am OK ******************');

        /* jshint ignore:start */

        $scope.tableParams = new ngTableParams({

            page: 1,            // show first page
            count: 10,          // count per page
            filter: {
                title: ''       // initial filter
            },
            sorting: {
                title: 'asc'     // initial sorting
            }
        }, {
            total: articles.length, // length of data
            getData: function($defer, params) {
                // use build-in angular filter
                var filteredData = params.filter() ?
                    $filter('filter')(articles, params.filter()) :
                    articles;
                var orderedData = params.sorting() ?
                    $filter('orderBy')(filteredData, params.orderBy()) :
                    articles;

                params.total(orderedData.length); // set total for recalc pagination
                $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
            }
        });
        /* jshint ignore:end */

        $scope.create = function() {
            var article = new Articles({
                title: this.title,
                content: this.content
            });
            article.$save(function(response) {
                $location.path('articles/' + response._id);
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });

            this.title = '';
            this.content = '';
        };

        $scope.remove = function(article) {
            if (article) {
                article.$remove();

                for (var i in $scope.articles) {
                    if ($scope.articles[i] === article) {
                        $scope.articles.splice(i, 1);
                    }
                }
            } else {
                $scope.article.$remove(function() {
                    $location.path('articles');
                });
            }
        };

        $scope.update = function() {
            var article = $scope.article;

            article.$update(function() {
                $location.path('articles/' + article._id);
            }, function(errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        };
    }
]);

Thank you in advance for your help.

Upvotes: 0

Views: 438

Answers (3)

Vaibhao
Vaibhao

Reputation: 31

Even your $scope.articles will not be available until you call the $scope.find method, so firstly you need to instantiate $scope.articles variable or just write call $scope.find() even before you declare $scope.tableParams = new ngTableParams({ }). This will work.

But for better code, wrap $scope.tableParams = new ngTableParams({ }) in a method and use ng-init to call $scope.find()

Upvotes: 0

zizzamia
zizzamia

Reputation: 241

Try to use #ngTasty http://zizzamia.com/ng-tasty/ it's way easier!

Upvotes: 1

mbroadst
mbroadst

Reputation: 888

It looks like your problem is where the interpreter told you it was:

total: articles.length, // length of data

should be

total: $scope.articles.length, // length of data

Of course, $scope.articles doesn't exist when you create your ngTable params, so you'll have to find some way to set that value at the right time (maybe in your getData method)

Upvotes: 0

Related Questions