14

I have made myself a custom pipe for filtering through my own objects, that is working properly when used with my dropdown selector. But I have noticed the filter gets run multiple times, by logging through the console everytime it runs.

Basic setup is a dropdown menu with elements, and then a list of objects that contain those elements. OnInit of my component I set the default selection for the dropdown. Any idea why my Filter will be running multiple times?

Even though it is working properly it is interfering with another filter because of it running multiple times.

2
  • Could you include your code/template to see how you are using the filter? Commented Sep 7, 2016 at 0:19
  • 1
    Angular refreshes the content quite often depending on application state. I wouldn't count on something being called just once. Commented Sep 7, 2016 at 0:51

1 Answer 1

23

If the pipe is pure (default) the pipe gets called when the input value or a parameter for the pipe has changed.

If the pipe is impure @Pipe({name: 'xxx', pure: false}), then the pipe is called every time change detection runs (which is usually quite often).

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

3 Comments

yep this is what i figured out, i actually just used another way where its not relying on being called only once. But the info you gave above is helpful to know. Thanks!
My impure pipe transform function gets called infinitely, does this mean that change detection is called infinitely or there is another problem in my app??
That change detection is called often is normal, but you have some expression (the one that contains the pipe) where change detection sees a change every time it checks. A guess is that you have a function or getter that returns a new instance of an object or array every time it is accessed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.