I am using a BehaviorSubject that acts as a state for a child component
state = new BehaviorSubject<any>({transform: 'translate3d(0, 0, 0)'});
styleState = this.state.asObservable();
In the child component template I subscribe to the state using the async pipe
<div>{{ styleState | async | json }}</div>
Then I update the child component state with the scroll event
ngOnInit() {
this.zone.runOutsideAngular(() => {
fromEvent(this.el.nativeElement, 'scroll').pipe(
tap((e: any) => {
this.zone.run(() => {
this.state.next({ scrollTop: e.target.scrollTop });
});
})
).subscribe();
});
}
Now in Parent component, the change detection is being triggered by the child component scroll event
ngAfterViewChecked() {
console.log('Parent View Checked!');
}
The problem is that the logic depends on the scroll event, and I don't want it to trigger a change detection in the parent component. knowing that I use OnPush change detection strategy in both parent and child component
Is this normal? how can I avoid it?
Here is a minimal reproduction https://stackblitz.com/edit/angular-hkwvy4?file=src%2Fapp%2Fhello.component.ts
'Parent View Checked!'if I didn't subscribe to the scroll event in the child - Murhaf Sousli