I’m attempting to inject NavController and ViewController into either my app.js file or a custom service.
My objective is (or was) to pass the current view into a “LeftMenuManager” service class. Some pages I need the leftmenu to be disabled, others I do not. The LeftMenuManager service would be responsible for flipping the switch based on the view name.
However, I’m unable to inject these dependencies - I get an error about “No provider for NavController…”.
I get the sense that I’m doing something backwards, but I don’t know exactly what. As most people, I’m new at this.
I understand that I’m attempting to inject controllers into non-pages. If this is the case, is there a central “master” page that will be called each time outside of the app.html?
Can someone please help me understand why I can’t do this? Or, if it’s possible, what it is that I’m missing?
import {NotificationInterface} from "./notification.interface";
import {Injectable} from "angular2/core";
import {Alert} from "ionic-framework/ionic";
import {NavController} from "ionic-framework/ionic";
@Injectable()
export class NotificationService implements NotificationInterface {
private nav;
constructor(nav: NavController) {
this.nav = nav;
}
alert(msg:string, title?:string) {
console.debug("alert:" + msg);
let alert = Alert.create({
title: title,
subTitle: msg,
buttons: ['Dismiss']
});
this.nav.present(alert);
}
}
This causes the same error as op is having. (The interface is just to make sure it has the alert func for when I want to have different providers to show notifications.
Not sure if this helps?
Also, my service used to work fine. Then I had the issue of multiple instances of other services and moved everything to the @App. Maybe that’s the problem, there might be no NavController yet at that level.
I realised above when I was writing this, So when you have this service in your @App you won’t get a NavController, make sure you provide the service at Component level, not at App.
import {NavController} from "ionic-framework/ionic";
and it work fine, just like:
import {NavController} from "ionic/ionic";
Regarding your other comments. You did well when you moved your service to root (app) level. Don’t forget also to import it in child components. And you are correct, NavController is not important for root component, as long as you set a this.root page.
Importing NavController isn’t the issue, the issue is when you attach it to the class with this.nav = nav in the constructor, weird thing is that it works in a Page component but not in an Injectable nor App component, is the @page decorator automatically including the NavController as a provider for each page?
Well i’m using @ViewChild and a variable in the html nav component to get it inside of the app.ts, idk if this is a good approach to the issue but maybe you could use it as well:
@praveenjp, my situation was similar to yours, I’ve solved using this post: Cannot resolve all parameters error?
Notice that the parameter now it’s just “nav”, cause you’re calling the one returned by the get parameters()
This question is asked every week, at least once ) So I strongly recommend you use search. It’s bad practice to change a view from a service (broken MVC).
However, you could send events from services to the main controller, and the controller can use NavController (best way), or you could send NavController to your service like an attribute (not bad way…). Or you may need to create a component instead of using the service.