性能上の理由から、私は自分のコンポーネントに対して手動の変更の検出を設定しようとしています。Angular2の観測可能な変更検出テンプレートの更新
アプリの構造:アプリケーション - >ブック(S) - >ページ(複数可)
私はAppComponentで観測可能に加入するには、私はその後、ChangeDetectorRefの "markForCheck" メソッドを実行します。
これはBookComponent(更新用にマークしたコンポーネントの子)でngAfterContentCheckedメソッドをトリガーしているようです。
これは、親コンポーネントの "markForCheck"もすべての子を更新すると考えました。
ただし、ngAfterContentCheckedが子(BookComponent)で実行されても、このコンポーネントのテンプレートは更新されません!
私はすべての子供(時にはコンポーネント階層のいくつかの深いところ)で観察可能なものを聞くはずですか?それとも私は何か悪いことをしましたか?そして、テンプレートが更新されないのに、なぜngAfterContentCheckedが子コンポーネントで実行されるのですか?
関連するコードのいくつか。
@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;
}
}
言葉は安いです - 私たちにいくつかのコードを示す;-)理想的にpl unkerは再現を可能にする。 –