0

I have Angular filter for paging (I used this link as tutorial):

publicApp.angularModule.filter('startFrom', function () {
return function(input, start) {
    start = +start; //parse to int
    return input.slice(start);
};

});

And relevant part of my HTML looks like this:

 <div data-ng-repeat="item in filteredData = ( data | filter:leaderBoardFilter) | startFrom:currentPage*pageSize | limitTo:pageSize">

And even though everything works fine and as expected I get this error in console:

"Error: input is undefined
@http://localhost:1936/MadbarzScripts/Public/PublicMain.js:8:9

Error is referencing to this line:

return input.slice(start);

And what is interesting I get error most of the time but not always. How can I fix this ?

hyperN
  • 2,674
  • 9
  • 54
  • 92

3 Answers3

3

A filter should always check the validity of its input. The input might be the result of an async operation, so the filter can't be sure the input is always what it's expecting for when it is being evaluated.

Try this:

publicApp.angularModule.filter('startFrom', function () {
    return function(input, start) {
        if (!angular.isArray(input)) {
            return [];
        }
        start = +start; //parse to int
        return input.slice(start);
    };
});
Kulbir Saini
  • 3,926
  • 1
  • 26
  • 34
Ye Liu
  • 8,946
  • 1
  • 38
  • 34
1

My guess is that your startFrom filter is taking 2 parameters but you are passing only 1 (currentPage*pageSize). Take a look at this:

How do I call an Angular.js filter with multiple arguments?

I am not sure why this happens intermittently but that could be a parameter passing/ordering quirk (given that you are trying to populate 2 arguments with one value).

Community
  • 1
  • 1
Sid
  • 7,511
  • 2
  • 28
  • 41
1

Needs to check the input exists or not:

Try this:

app.filter('startFrom', function() {
return function(input, start) {
    if (!input || !input.length) { return; }
    start = +start; //parse to int
    return input.slice(start);
}
});

Hope its work..Cheers..

Amarendra Kumar
  • 858
  • 9
  • 16