How to avoid multiple execution of unclean piping in angular 2?

Hi, I use Angular 2 pipe to return the keys of an object, it is an unclean channel and it is executed several times, which blocks some other script, how can I avoid several executions of unclean pipes? my code is as follows:
 import {Pipe,PipeTransform} from '@angular/core';
    @Pipe({ name: 'NgforObjPipe', pure: true })
    export class NgforObjPipe implements PipeTransform {
        transform(value, args:string[]):any {
        let keys = [];
        for (let key in value) {
            keys.push({key: key, value: value[key]});
        }
        console.log('pipeeeeeeeeeeeeeee', keys);
        return keys;
        }
    }
+4
source share
2 answers

This cannot be prevented. What is an unclean pipe for?

What can you do to make sure that there is as little work inside the method as possible transform(), for example, by caching the results and only recount when the passed value has really changed.

Object.keys() . . [ ] * ngFor Angular 2

ChangeDetectionStrategy.OnPush , .

+3

.

  • Input ChangeDetectionStrategy onPush.
  • NgforObjPipe collection , , NgforObjPipe.tranform().
+2

Source: https://habr.com/ru/post/1682178/


All Articles