我在试着学习安古拉杰。我使用filter函数来排序一些项目,如下所示:
angular.module('reverseDirective', [])
.filter('reverse', function() {
return function(items) {
return items.slice().reverse();
}
});当我把它放到html文件中时
<div class="panel-body" ng-repeat="each in story.stories | reverse">
<p>{{ each.content }}</p>控制台总是显示此错误:
TypeError: Cannot read property 'slice' of undefined
at http:localhost:3000/app/directives/reverse.js:6:15
at fn (eval at <anonymous> (http:localhost:3000/js/angular.min.js:212:83), <anonymous>:4:203)
at Object.<anonymous> (http:localhost:3000/js/angular.min.js:117:376)
at n.$get.n.$digest (http:localhost:3000/js/angular.min.js:132:124)
at n.$get.n.$apply (http:localhost:3000/js/angular.min.js:135:269)
at l (http:localhost:3000/js/angular.min.js:87:152)
at F (http:localhost:3000/js/angular.min.js:91:187)
at XMLHttpRequest.K.onload (http:localhost:3000/js/angular.min.js:92:220)我找了又试,但还是找不到解决方案,所以我在等待。
发布于 2015-11-22 02:03:12
您应该检查项目是否存在。这个解决方案应该对您有效。
angular.module('reverseDirective', [])
.filter('reverse', function() {
return function(items) {
if (!items) {
return;
}
return items.slice().reverse();
}
});发布于 2015-08-13 10:11:13
你为什么不使用内置的过滤器orderBy。如果需要,您可以向它传递一个函数。检查文档的http://docs.angularjs.org/api/ng.filter:orderBy。它还支持反向功能。
https://stackoverflow.com/questions/31978330
复制相似问题