我使用ngTable (AngularJS)显示元素列表,但编译时出错:
*我没事*
articles.client.controller.js:12 ReferenceError:文章未在新的(./模块/条款/控制器articles.client.controller.js:27:16)中定义
这是我的代码:myApp/public/模组/文章/控制器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;
});
};
}
]);提前谢谢你的帮助。
发布于 2014-09-16 05:13:08
试着使用#ngTasty http://zizzamia.com/ng-tasty/ --这样就更容易了!
发布于 2014-05-19 19:49:36
看起来你的问题就在翻译告诉你的地方:
total: articles.length, // length of data应该是
total: $scope.articles.length, // length of data当然,$scope.articles在创建ngTable参数时并不存在,因此您必须找到一些方法在正确的时间设置该值(可能在getData方法中)。
发布于 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()。
https://stackoverflow.com/questions/23743079
复制相似问题