Custom Filter Giving "cannot Read Property 'slice' Of Undefined" In Angularjs
My custom startFrom filter is giving me an error. app.filter('startFrom', function() { return function(input, start) { start = +start; //parse to int return inp
Solution 1:
Your filter needs to check whether or not the input exists:
app.filter('startFrom', function() {
returnfunction(input, start) {
if (!input || !input.length) { return; }
start = +start; //parse to int
returninput.slice(start);
}
});
Otherwise, the filter function will run and thus call slice on undefined which doesn't have a property of slice like an array or string does.
The reason the filter is called while there is no value is because the filter will run when Angular runs its first $digest cycle. You could avoid the error by adding an initial value in the controller, but it's best just to add the if statement to the filter.
Here's a demo of both solutions. Notice there are no errors.
Post a Comment for "Custom Filter Giving "cannot Read Property 'slice' Of Undefined" In Angularjs"