Unable to import NavController into service class

I am trying to import NavController in to my service class, when i call method from my service class i am getting below error

Unhandled Promise rejection: No provider for NavController! ; Zone: ; Task: Promise.then ; Value: NoProviderError {nativeError: Error: No provider for NavController!
at NoProviderError.BaseError [as constructor] (http://loca…, keys: Array[1], injectors: Array[1]} Error: No provider for NavController!
at NoProviderError.BaseError [as constructor] (http://localhost:8100/build/main.js:14434:34)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:8100/build/main.js:63853:16)
at new NoProviderError (http://localhost:8100/build/main.js:63884:16)
at ReflectiveInjector
.throwOrNull (http://localhost:8100/build/main.js:122959:19)
at ReflectiveInjector
.getByKeyDefault (http://localhost:8100/build/main.js:122987:25)
at ReflectiveInjector
.getByKey (http://localhost:8100/build/main.js:122950:25)
at ReflectiveInjector
.get (http://localhost:8100/build/main.js:122759:21)
at AppModuleInjector.createInternal (/AppModule/module.ngfactory.js:573:57)
at AppModuleInjector.NgModuleInjector.create (http://localhost:8100/build/main.js:64561:76)
at NgModuleFactory.create (http://localhost:8100/build/main.js:64546:18)

here is my service class

import { Injectable } from ‘@angular/core’;
import { Http } from ‘@angular/http’;
import ‘rxjs/add/operator/map’;
import { NavController } from ‘ionic-angular’;
@Injectable()
export class Navigator {
constructor(
public navCtrl: NavController,
){}
toPage(toPage){
this.navCtrl.push(toPage);
}
}

since iam new to ionic 2 can anyone help pls?

This is just my personal opinion, and perhaps not widely shared, but I think this is better avoided, and I would look for a way to achieve your goal that does not involve injectable services trying to interact with navigation controllers.

2 Likes