How to use $ionicPopup from inside app js (not controller)


I have javascript classes written in logic.js. How do they use $ionicPopup? Do I have to inject it from the controller and then pass it to logic.js objects? Seems a bit artificial. Is there anyway to make $ionicPopup globally available.



Why you don’t make a service ?

However you can put $ionicPopup in the ‘window’ object to access globally.


Where should I write the code to do this? I am new to angularjs.


Make $ionicPopup globally available in the sense of having a global variable (i.e. a property of “window”) ?

No that’s not a recommended practice, it goes against “the Angular Way”.

What you can do is create a Service for this. The service gets $ionicPopup injected and has a method to show the popup. Then in your controllers you just need to inject the service and you can call the popup function.

Now what you can do is you can add other utility functions to this service, for instance a function to show and hide $ionicLoading. Just a bunch of “utility” things that you often need.

Now the nice thing is that, in your controllers, you only need to inject this “Util” service and then you can access all of that functionality, instead of having to inject a bunch of separate dependencies in your controllers.

Here is an example of a “utility style” service which follows this pattern:

If you’re interested you can have a look at my starter app which has a number of angularjs best practices built-in:

The README has instructions on how to install and use it.


I’m curious about this as well. In my current app I’m injecting $ionicPopup all over the place to to show an alert message. On some pages, I have a $ionicPopup.then(…) promise callback, which is a wrapper around $ionicHistory.goBack(-1) and on some pages i don’t. Is this the best practice or is there a way to define it globally?

(When I say globally, I don’t mean sticking stuff on $rootScope or window object).


hello, im new to angular and ionic. i cant really understand hot to implement this. i am studying from here

and i can understand a little, but in my case i dont know. what ionicPopup suppose to return…?