如何訪問根組件以獲取更改屬性?角度4:訪問根組件
問題:
我有差異的模板(默認和自定義頁面),而這些模板具有不同的結構。我希望控件通過根組件(AppComponent)類中的自定義參數呈現過程。
舉個例子:
app.component.html:
<div>
<div id="wrapper">
<div *ngIf="!customPage">
<div id="page-wrapper">
<div class="container-fluid">
<router-outlet></router-outlet>
</div>
</div>
</div>
<div *ngIf="customPage">
<router-outlet></router-outlet>
</div>
</div>
</div>
app.component.ts:
export class AppComponent{
customPage: boolean;
constructor() {
this.customPage = false;
}
}
我試着從自定義訪問.page.ts:
export class CustomComponent implements AfterViewChecked {
constructor(private rootComponent: AppComponent) {
}
ngAfterViewChecked(): void {
this.rootComponent.customPage = true;
}
}
不過我檢索錯誤:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
我嘗試使用其他生命週期事件,但在所有的事件,我檢索此錯誤。
而且,我覺得這個解決方案不好。也許Angular2爲這個問題提供瞭解決方案?
謝謝。
的可能的複製[角2 - 共享服務在加載](https://stackoverflow.com/questions/45443908/angular-2-share-service-on-load) –
可以使用'[隱藏]'而不是'* ngIf' –