From e89f3b0206ac48ef64d43cc5cf415187e0415a84 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Mon, 24 Oct 2016 23:12:07 +0200 Subject: [PATCH] fix(nav): ionViewDidLoad fires before children components have been loaded fixes #8449 --- src/components/nav/test/basic/app-module.ts | 58 +++++++++++++++++++-- src/navigation/nav-controller-base.ts | 8 +-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/components/nav/test/basic/app-module.ts b/src/components/nav/test/basic/app-module.ts index 4d66652cfa2..9cd47bc09b5 100644 --- a/src/components/nav/test/basic/app-module.ts +++ b/src/components/nav/test/basic/app-module.ts @@ -1,11 +1,20 @@ import { NgModule, Component, ViewChild } from '@angular/core'; -import { App, AlertController, Content, DeepLinkConfig, IonicApp, IonicModule, NavController, NavParams, Tabs, Tab, ModalController, ViewController } from '../../../..'; +import { App, AlertController, Content, DeepLinkConfig, IonicApp, IonicModule, Label, NavController, NavParams, Tabs, Tab, ModalController, ViewController } from '../../../..'; @Component({ selector: 'my-cmp', - template: `

My Custom Component Test

` + template: `My Custom Component Test + {{value}}` }) -export class MyCmpTest {} +export class MyCmpTest { + @ViewChild(Label) _label: Label; + label: Label; + value: string = ''; + + ngOnInit() { + this.label = this._label; + } +} @Component({ @@ -22,6 +31,16 @@ export class MyCmpTest {} +
+

ionViewCanEnter ({{called.ionViewCanEnter}})

+

ionViewCanLeave ({{called.ionViewCanLeave}})

+

ionViewDidLoad ({{called.ionViewDidLoad}})

+

ionViewWillEnter ({{called.ionViewWillEnter}})

+

ionViewDidEnter ({{called.ionViewDidEnter}})

+

ionViewWillLeave ({{called.ionViewWillLeave}})

+

ionViewDidLeave ({{called.ionViewDidLeave}})

+ +
{{title}} @@ -53,7 +72,6 @@ export class MyCmpTest {} -
` }) export class FirstPage { @@ -61,39 +79,62 @@ export class FirstPage { title = 'First Page'; pages: Array = []; @ViewChild(Content) content: Content; + @ViewChild(MyCmpTest) myCmp: MyCmpTest; canLeave = true; + called: any; constructor( public navCtrl: NavController, public viewCtrl: ViewController, public alertCtrl: AlertController - ) {} + ) { + this.called = { + ionViewCanEnter: 0, + ionViewCanLeave: 0, + ionViewDidLoad: 0, + ionViewWillEnter: 0, + ionViewDidEnter: 0, + ionViewWillLeave: 0, + ionViewDidLeave: 0 + }; + } ionViewDidLoad() { console.log('ionViewDidLoad, FirstPage'); for (var i = 1; i <= 50; i++) { this.pages.push(i); } + if (!this.myCmp || !this.content || !this.myCmp.label) { + throw new Error('children are not loaded'); + } + this.myCmp.value = 'root!'; + this.myCmp.label.color = 'primary'; + this.called.ionViewDidLoad++; } ionViewWillEnter() { console.log('ionViewWillEnter, FirstPage', this.viewCtrl.id); + this.called.ionViewWillEnter++; } ionViewDidEnter() { console.log('ionViewDidEnter, FirstPage', this.viewCtrl.id); + this.called.ionViewDidEnter++; } ionViewWillLeave() { console.log('ionViewWillLeave, FirstPage', this.viewCtrl.id); + this.called.ionViewWillLeave++; } ionViewDidLeave() { console.log('ionViewDidLeave, FirstPage', this.viewCtrl.id); + this.called.ionViewDidLeave++; } ionViewWillUnload() { console.log('ionViewWillUnload, FirstPage', this.viewCtrl.id); + this.called.ionViewWillUnload++; } ionViewCanLeave() { @@ -106,9 +147,16 @@ export class FirstPage { alert.addButton({ text: 'Umm, ok', role: 'cancel', }); alert.present(); + this.called.ionViewCanLeave++; + return false; } + ionViewCanEnter() { + this.called.ionViewCanEnter++; + return true; + } + setPages() { let items = [ { page: PrimaryHeaderPage } diff --git a/src/navigation/nav-controller-base.ts b/src/navigation/nav-controller-base.ts index e20f82b42e1..c921c8d806f 100644 --- a/src/navigation/nav-controller-base.ts +++ b/src/navigation/nav-controller-base.ts @@ -425,10 +425,6 @@ export class NavControllerBase extends Ion implements NavController { _viewAttachToDOM(view: ViewController, componentRef: ComponentRef, viewport: ViewContainerRef) { assert(view._state === ViewState.INITIALIZED, 'view state must be INITIALIZED'); - // successfully finished loading the entering view - // fire off the "didLoad" lifecycle events - this._didLoad(view); - // render the component ref instance to the DOM // ******** DOM WRITE **************** viewport.insert(componentRef.hostView, viewport.length); @@ -443,6 +439,10 @@ export class NavControllerBase extends Ion implements NavController { } componentRef.changeDetectorRef.detectChanges(); + + // successfully finished loading the entering view + // fire off the "didLoad" lifecycle events + this._didLoad(view); } _viewTest(enteringView: ViewController, leavingView: ViewController, ti: TransitionInstruction) {