angularangular-pipeangular2-pipe

What is an impure pipe in Angular?


@Pipe({name:'myPipe', pure: false})

I am unable to understand impure pipes.

What's the difference between pure and impure pipes?

Please explain with a simple and basic example.


Solution

  • A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe.

    An impure pipe is called for every change detection cycle no matter whether the value or parameter(s) changes.

    This is relevant for changes that are not detected by Angular

    In these cases you probably still want the pipe to be executed.

    You should be aware that impure pipes are prone to be inefficient. For example when an array is passed into the pipe to filter, sort, ... then this work might be done every time change detection runs (which is quite often especially with the default ChangeDetectionStrategy setting) event though the array might not even have changed. Your pipe should try to recognize this and for example return cached results.