Firstly: Why is it a problem that your component renders a lot of times? You should not have any performance issues, since *ngIf
is designed for those operations.
The reason why your component renders so often because Angular triggers ChangeDetection
whenever something changes in your component. Then, all variables, directives in your view are checked if something changed.
What you could do in order to avoid that is to prevent doing that in your component decorator by adding another changeDetectionStrategy like this:
@Component({
// ...other ocmponent decorator properties
changeDetection: ChangeDetectionStrategy.OnPush
})
Now your component only triggers changeDetection if your @Input()
parameters change, or if you specifically tell it to.
So now, whenever you change your filters (or do something else in your component which is not triggered through @Input()
parameters, you need to trigger changeDetection manually.
Therefore, first inject it via DependencyInjection in your constructor:
constructor(private cd: ChangeDetectorRef) {}
Then, call it after you executed the change:
this.cd.detectChanges();
Hope that helps.