【问题标题】:Error ngTable AngularJS错误ngTable AngularJS
【发布时间】:2014-05-19 16:47:36
【问题描述】:

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

提前感谢您的帮助。

【问题讨论】:

    标签: angularjs ngtable referenceerror


    【解决方案1】:

    尝试使用#ngTasty http://zizzamia.com/ng-tasty/ 更简单!

    【讨论】:

      【解决方案2】:

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

      total: articles.length, // length of data
      

      应该是

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-23
          • 1970-01-01
          • 1970-01-01
          • 2014-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多