Friday, 27 September 2013

Nested list in angular.js filters

Nested list in angular.js filters

I'm trying to create a simple pagination filter for angular, to be used
like so:
<ul>
<li ng-repeat="page in items | paginate: 10">
<ul>
<li ng-repeat="item in page">{{ item }}</li>
</ul>
</li>
</ul>
I've written simple function:
angular.module('app.filters', [])
.filter('paginate', function() {
return function(input, perPage) {
var pages = [],
perPage = perPage || 10;
for (i=0; i < input.length; i += perPage)
pages.push(input.slice(i, i + perPage));
return pages;
};
});
And it caused angular to crash with quite cryptic (for me at least) error
messages. I figured out that the problem is in nested lists in filter
results. To reproduce the problem, it's enough to do like this:
angular.module('app.filters', [])
.filter('paginate', function() {
return function(input, perPage) {
return [[1,2,3],[4,5,6]];
};
});
Can you please tell me:
why nested lists are a problem to angular filters?
where can i read about it in documentation?
how can i eventually write a filter in correct way?
You can see all code in this plunker:
http://plnkr.co/edit/gUIJcJg0p5LqKGH10B8t?p=preview After running the
code, open the console, you'll see error messages.
Thank you

No comments:

Post a Comment