angular custom filter issue
NickName:user3127896 Ask DateTime:2015-08-04T21:27:28

angular custom filter issue

I am trying to implement custom filter in angular js. The idea is that user can add some tags and each time filter is invokes.

Filter is a plain javascript object, basically it looks like this:

var filter = {color:'blue', length: 15};

Items are array of objects.

app.filter('filterByTags', function () {
    return function (items, filter) {
        debugger;
        if (!isEmpty(filter)) {
            var filtered = items;
            for (var prop in filter) {
                if (filter.hasOwnProperty(prop)) {
                    for (var i = 0; i < filtered.length; i++) {
                        if (filtered[i][prop] !== filter[prop]) {
                            filtered.splice(filtered.indexOf(filtered[i]), 1);
                        }
                    }
                }
            }
            alert('Return filtered items');
            return filtered;
        } else {
            alert('Original items ');
            return items;
        }
    };
});

So if works fine when i add tag but when i remove this tag i should get initial items list, but i get already filtered list.

So for instance originally i have a list of 5 items and when i click some tag I should get 4 items. But when I remove this tag i should get 5 items again, but i still get 4 items.

I think it is because in this line of code:

return function (items, filter) {

when filter invokes second time, items are already filtered and returned list before. Any ideas how can i fix it?

Copyright Notice:Content Author:「user3127896」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31810636/angular-custom-filter-issue

More about “angular custom filter issue” related questions

angular custom filter issue

I am trying to implement custom filter in angular js. The idea is that user can add some tags and each time filter is invokes. Filter is a plain javascript object, basically it looks like this: var

Show Detail

Custom filter in Angular

I have some problem with custom filter. I can't include it's in my project. Firstly I used a filter: text. I understand that array initialized asynchronously and used this custom filter. But when i

Show Detail

Issue with angular translation in custom filter

I am trying to create a filter for showing the elapsed date and time in angular js and is working fine. Now I am trying to add translation to it but that time its showing the keys instead of the v...

Show Detail

How to pass multiple parameter in angular filter function, not custom filter

I tried hard and visit lot of similar question like this but still unable to solve this issue. I want to pass extra parameter in angular filter function. I found solution as below but it's not wor...

Show Detail

Issue with passing parameters in angular js custom filter

I'm facing some issues with passing parameters in angular js custom filter. I've the following search bar and ng-repeat :- &lt;input type="text" placeholder="Search your contacts" ng-model="query...

Show Detail

Passing a custom filter with attributes to a custom directive in angular

Is there a way to pass a custom filter as an attribute into a custom directive? So if I have a directive &lt;my-element value="1234" filter="my-filter:attr1:attr2"&gt;&lt;my-element&gt; as ang

Show Detail

Typescript: How to utilize angular $filter within custom filter

How can I utilize angular $filter within a custom filter? How to inject $filter dependency? module Filters { export class CustomFilter { public static Factory() { return

Show Detail

Custom Angular filter never called?

I have an array of strings which I'd like to filter by first letter using a custom angular filter. I have the following ng-repeat set up: &lt;div ng-repeat="proverb in proverbs | firstLetter"&gt;...

Show Detail

Creating a custom Angular filter with TypeScript

I'm trying to work out the best way of creating a custom Angular Filter with TypeScript. All the code samples I see use something like: myModule.filter( "myFilter", function() { return functi...

Show Detail

Custom comparator for a single property for Angular $filter

First, my reason for creating the custom comparator in the first place is because an attribute on the expression that is an array--such as 'tags'--required the expression to have the values in a

Show Detail