NavController Reference in module constructor


#1

I organized my application in feature modules using @NgModule.

I want to use the module constructor to do some initialization needed for each module (e.g.: Registering for relevant events at a central notification module). Now, to handle specific events, the module has to push pages to the navigation controller. I am able to get references to any service or controller, but I cannot access the NavController in the module constructor.

Example:

@NgModule({
	 imports: [
		IonicModule,
		SharedModule,
		NotificationModule
	],
	declarations: [
		XXDetailsPage,
		XXParticipantListPage,
	],
	entryComponents: [
		XXDetailsPage,
		XXParticipantListPage,
	],
	providers: [],
	exports: []
})
export class XXModule {
	constructor(
		@Inject('NotificationProvider') notificationProvider: INotificationProvider,
		navCtrl: NavController // <<<<<<<<<<<<<< not possible
	) {
		// register notification handlers
		notificationProvider.registerNotificationInteractionHandler("xx.NewRequest", (notification: Notification) => {
			navCtrl.push(XXDetailsPage, notification.data.xx);
			navCtrl.push(XXParticipantListPage, notification.data.xx);
		});
	}
}

Now I’m looking for another way to get a reference to the NavController. In the docs, I found this hint: http://ionicframework.com/docs/v2/components/#navigating_from_root which explains why I cannot inject it here, but unfortunately I still have no idea how to get a reference.

Any ideas?

Alternatively, I could provide some kind of init() Method on the module which does the initialization, which I could call from the Root Module and pass the NavController as parameter. But how could I get a reference to the module object?


#2

Ok i got the workaround i mentioned in the last sentence working. I’m still interested in a more clean solution using constructor, but in case this is not possible you can just do:

@NgModule({
	 imports: [
		IonicModule,
		SharedModule,
		NotificationModule
	],
	declarations: [
		XXDetailsPage,
		XXParticipantListPage,
	],
	entryComponents: [
		XXDetailsPage,
		XXParticipantListPage,
	],
	providers: [],
	exports: []
})
export class XXModule {
	constructor(
		@Inject('NotificationProvider') private notificationProvider: INotificationProvider
	) {
            }
	
	init(navCtrl: NavController) {
		// register notification handlers
		this.notificationProvider.registerNotificationInteractionHandler("xx.NewRequest", (notification: Notification) => {
			navCtrl.push(XXDetailsPage, notification.data.xx);
			navCtrl.push(XXParticipantListPage, notification.data.xx);
		});
	}
}

And in your main app component just add:

constructor(
            //.....
            private xxModule: XXModule
) {
ngAfterViewInit() {
    this.xxModule.init(this.nav);
}