0

我正在使用 ngTable (AngularJS) 来显示元素列表,但在编译时出现错误:

************ 我很好 ******************

articles.client.controller.js:12 ReferenceError:文章未在新定义(...../modules/articles/controllersarticles.client.controller.js:27:16)

这是我的代码:myApp/public/modules/articles/controllersarticles.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;
            });
        };
    }
]);

预先感谢您的帮助。

4

3 回答 3

1

尝试使用#ngTasty http://zizzamia.com/ng-tasty/它更容易!

于 2014-09-16T05:13:08.773 回答
0

看起来你的问题出在口译员告诉你的地方:

total: articles.length, // length of data

应该

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

当然, $scope.articles 在您创建 ngTable 参数时不存在,因此您必须找到某种方法在正确的时间设置该值(可能在您的 getData 方法中)

于 2014-05-19T19:49:36.590 回答
0

在您调用该方法之前,即使您$scope.articles也将不可用$scope.find,因此首先您需要在声明之前实例化$scope.articles variable或只编写调用。这将起作用。$scope.find()$scope.tableParams = new ngTableParams({ })

但是为了更好的代码,包装$scope.tableParams = new ngTableParams({ })一个方法并使用ng-init来调用$scope.find()

于 2015-05-07T19:38:46.783 回答