0

I am learning Angular so I created casual To do app. Custom filter works on localhost while on hosting it doesnt. I don't get any error messages and data doesn't show. If i remove taskPriority:this from li data shows which means on the server everything is ok but then, of course, filtering doesn't work. I am passing scope (this) to filter.

<li ng-repeat="task in tasks | taskPriority:this | filter:taskname as ukupno track by $index">
    <input class="edit_task" ng-blur="editTask(task.id, task.task)" ng-model="task.task" value="{{ task.task }}" type="text">
</li>

Filter checks if there is priority level (1,2,3) in array fSPriorites matching priority in task, if yes then it returns those tasks.

angular.module('TaskFilter', []).filter('taskPriority', function() {
    return function(task, scope) {
        var filtered = [];
        angular.forEach(task, function(task) {
            if($.inArray(task.priority, scope.fSPriorites) != -1)
                filtered.push(task);
        });
        return filtered;
    };
});

How do i debug this?

2
  • 1
    try changing <li ng-repeat="task in ukupno = (tasks | taskPriority:this | filter:taskname) track by $index"> Commented Feb 8, 2015 at 12:02
  • unfortunately, no results on hosting server although its working allright on local. Commented Feb 8, 2015 at 12:16

1 Answer 1

1

I don't understand why you are passing scope to fileter. This is not a good practice. Try below code.

HTML

<li ng-repeat="task in tasks | taskPriority:this.fSPriorites | filter:taskname as ukupno track by $index">
    <input class="edit_task" ng-blur="editTask(task.id, task.task)" ng-model="task.task" value="{{ task.task }}" type="text">
</li>

Filter

angular.module('TaskFilter', []).filter('taskPriority', function() {
    return function(task, fSPriorites) {
        var filtered = [];
        angular.forEach(task, function(task) {
            if($.inArray(task.priority, fSPriorites) != -1)
                filtered.push(task);
        });
        return filtered;
    };
});

Hope this could help you.Thanks,

Sign up to request clarification or add additional context in comments.

4 Comments

It still doesnt work. i get no data angular-laravel.wc.lt/#/tasks but if I remove call to custom filter from li tag it works. postimg.org/image/gqj7ouck7 Really strange, but I'm doing this just for practice so it isn't a big deal. thank you for your time
@Marko may be response is wrong on server..or something bad happen with fSPriorites variable of scope. it may be null or undefined..that may causing if($.inArray(task.priority, fSPriorites) != -1) to false and no records gets filtered
the problem was that for some reason inArray (and indexOf which I also tried to use) didn't work properly. finally its working with for each loop. ty
In addition $.inArray will never work in IE8..Thats cool fix..Feeling glad to help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.