首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >误差ngTable AngularJS

误差ngTable AngularJS
EN

Stack Overflow用户
提问于 2014-05-19 16:47:36
回答 3查看 431关注 0票数 0

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

*我没事*

articles.client.controller.js:12 ReferenceError:文章未在新的(./模块/条款/控制器articles.client.controller.js:27:16)中定义

这是我的代码:myApp/public/模组/文章/控制器articles.client.controller.js

代码语言:javascript
复制
'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;
            });
        };
    }
]);

提前谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2014-09-16 05:13:08

试着使用#ngTasty http://zizzamia.com/ng-tasty/ --这样就更容易了!

票数 1
EN

Stack Overflow用户

发布于 2014-05-19 19:49:36

看起来你的问题就在翻译告诉你的地方:

代码语言:javascript
复制
total: articles.length, // length of data

应该是

代码语言:javascript
复制
total: $scope.articles.length, // length of data

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

票数 0
EN

Stack Overflow用户

发布于 2015-05-07 19:38:46

在调用$scope.articles方法之前,即使您的$scope.find也是不可用的,所以首先需要实例化$scope.articles variable,或者在声明$scope.tableParams = new ngTableParams({ })之前只编写call $scope.find()。这会管用的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23743079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档