For performance reasons, I am trying to set up manual change detection on my components.
The structure of the app: App -> Book(s) -> Page(s)
I subscribe to an observable in the AppComponent, I then run the "markForCheck" method of the ChangeDetectorRef.
This seems to trigger the ngAfterContentChecked method in the BookComponent (the child of the component I marked for update).
This made me believe that "markForCheck" in a parent component also updates all the children.
However, even though ngAfterContentChecked is executed in the child (BookComponent), the template of this component doesn't update!
Am I supposed to listen to the observable in every child (sometimes several levels deep in the component hierarchy)? Or did I do something wrong? And why does ngAfterContentChecked execute in the child components even though the template doesn't update?
Some of the relevant code.
@Component({
selector: 'app',
directives: [BookComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<book *ngIf="_book" [book]="_book" [style.height]="_availableDimensions.height+'px'" (window:resize)="onResize($event)"></book>
<footer id="footer"></footer>
`
})
export class PlayerComponent {
ngOnInit() {
this.initScale();
}
initScale(){
this._playerService.availableDimensions$.subscribe(
availableDimensions => {
// set local availableDimensions variable
this._availableDimensions = availableDimensions;
this._cd.markForCheck();
}
);
}
}
@Component({
selector: 'book',
directives: [PageComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div id="pageScrollZone">
<div id="pageHolder" [ngStyle]="pageHolderStyles()"></div>
</div>
`
})
export class BookComponent {
constructor(
private _cd: ChangeDetectorRef,
private _playerService: PlayerService,
private _config: Config
){}
ngAfterContentChecked(){
// This part does execute when the observable changes
let date = new Date();
console.log('BOOK: '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds());
}
pageHolderStyles(){
// This part does NOT execute when the observable changes
let styles = {
marginTop: this._currentMargin.y+'px',
transform: 'scale('+ (this._baseZoom * this._currentZoomLevel) +')'
}
return styles;
}
}