June 20, 2016

Ionic 2 use NavController in Service to show Alerts

Usually you inject Nav:NavController into your classes constructor to use it:

But if you are working in a service you will receive the following error:

If you want to show alerts from within a service you are required to use the NavController.  To do so simply create the private field nav with type NavController. Then use the app.getActiveNav method provided by ionic-angular. See the snippet below for a full example:

Now you can show dialogs and alerts from a service in Ionic 2. But keep in mind, this is not good practice – services should not be tied to the UI of your application.

Related Posts

Avatar photo

theCodeCampus
Developer at thecodecampus </>

Our knowledge is not simply gained through reading - it is trained, tested and constantly being expanded. Because first and foremost, we are all developers at W11K. The know-how that we acquire here as developers, consultants and information architects flows immediately into our training courses and articles for theCodeCampus.


50 responses to “Ionic 2 use NavController in Service to show Alerts”

  1. Alugbin Abiodun says:

    Please can we get the javascript version?? it keeps giving no provider for App!!

  2. Can Kattwinkel says:

    Hello Alugbin Abiodun,

    first of all – developing a Ionic 2 app with JavaScript is a BIG mistake. Take 3 days to learn the necessary TypeScript it will save you way more time.

    However you should be able to use app.getActiveNav(); in JavaScript as well, just follow the imports in the code snippet. I couldn’t setup a JavaScript App with ionic since the startup repositories are now fully in TypeScript.

  3. Brock Ellis says:

    Hi there!

    This blog post was very helpful!

    I have an ErrorService that I was using to catch errors thorough the application and display an alert. I know that this is bad practice (as stated by many other blogs as well) but I have never seen a good example of error handling and display?

    Where should the error UI (AlertController and such) go? How could you set up a global error class to be reusable between all components, if that’s even possible.

    Thanks so much for your help!

  4. Venkata says:

    The nav.present method I do not see in the ionic2 now. was that removed?
    Can you please explain any other way I can do that?

  5. Thanks man, it worked here!

  6. Chikku says:

    Thank you great explanation!

  7. Andrey says:

    Thank you so much! This is solution for my problem!

  8. zuan says:

    Thank you for the useful update For beginners and start your career in Ionic.

  9. shreyaz says:

    Thanks for giving error and nav controller code. These code are usefull ionic developer.

  10. aruna ram says:

    Nice Blog! Thanks for providing the knowledgeable content, it was a great post to developing my skills. Well done…!

  11. Very interesting and useful information, thanks for sharing with us, keep up the good work!

  12. Nice Blog! Thanks for providing the knowledgeable content

  13. Very Nice!! Thanks for Sharing..

  14. Gautami says:

    Very Nice!! Thanks for Sharing..

  15. Very Usefull Infoemation!! Thanks

  16. Thanks for giving useful Information….

  17. Nice Blog!!!! Thanks for sharing this with us.

  18. Recruitment Consultants says:

    Great, thanks for sharing.

  19. Tremendous blog quite easy to grasp the subject since the content is very simple to understand. Obviously, this helps the participants to engage themselves in to the subject without much difficulty. Hope you further educate the readers in the same manner and keep sharing the content as always you do.

  20. Stupendous blog huge applause to the blogger and hoping you to come up with such an extraordinary content in future. Surely, this post will inspire many aspirants who are very keen in gaining the knowledge. Expecting many more contents with lot more curiosity further.

  21. Soclikes says:

    Thank you for sharing this! I learn how to use ionic 2 now

  22. Viplikes says:

    Vielen Dank für dieses Beispiel! Es hat mir geholfen

  23. Extraordinary blog went amazed with the content that they have developed in a very descriptive manner. This type of content surely ensures the participants to explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

  24. Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

  25. Honestly speaking this blog is absolutely amazing in learning the subject that is building up the knowledge of every individual and enlarging to develop the skills which can be applied in to practical one. Finally, thanking the blogger to launch more further too.

  26. Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

  27. Truly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.

  28. Highly appreciable regarding the uniqueness of the content. This perhaps makes the readers feels excited to get stick to the subject. Certainly, the learners would thank the blogger to come up with the innovative content which keeps the readers to be up to date to stand by the competition. Once again nice blog keep it up and keep sharing the content as always.

  29. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.

  30. ProWeb365 says:

    Very detailed and informative guide. Learned a lot of new things about.

  31. Extraordinary blog filled with an amazing content which no one has touched this subject before. Thanking the blogger for all the terrific efforts put in to develop such an awesome content. Expecting to deliver similar content further too and keep sharing as always.

  32. Stupendous blog huge applause to the blogger and hoping you to come up with such an extraordinary content in future. Surely, this post will inspire many aspirants who are very keen in gaining the knowledge. Expecting many more contents with lot more curiosity further.

  33. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.

  34. Extraordinary blog filled with an amazing content which no one has touched this subject before. Thanking the blogger for all the terrific efforts put in to develop such an awesome content. Expecting to deliver similar content further too and keep sharing as always.

  35. Krootez says:

    That works! Thanks for sharing it

  36. Extraordinary blog went amazed with the content that they have developed in a very descriptive manner. This type of content surely ensures the participants to explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

  37. Impressive blog to be honest definitely this post will inspire many more upcoming aspirants. Eventually, this makes the participants to experience and innovate themselves through knowledge wise by visiting this kind of a blog. Once again excellent job keep inspiring with your cool stuff.

  38. Must read article by everyone including a valid content which educate folks with its unique blog. Thus, exploring the readers curiosity in gaining a valuable content. Thanks to the blogger.

  39. Must read article by everyone including a valid content which educate folks with its unique blog. Thus, exploring the readers curiosity in gaining a valuable content. Thanks to the blogger.

  40. nayak says:

    Thanks for a blog in very useful as my skill and my work.keep up…..

  41. Nidhiverma says:

    That’s very unique code i ever seen.

  42. i really loved this blog this is the best blog I have ever read in my life, thank you so much for sharing this post.

  43. Thanks for sharing this informative article to us. Keep sharing more related blogs.

  44. Nice information, Thanks for sharing this.

  45. Such a great and useful blog for me. Thanks for sharing this very informative blog.

  46. Thank You for This Wonderful blog. I really enjoyed reading your blog hope you keep posting more such blogs often.

  47. Misha says:

    I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.

Leave a Reply

Add code to your comment in Markdown syntax.
Like this:
`inline example`

```
code block
example
```

Your email address will not be published.