Using $ionicLoading inside a factory

Hi,

Im trying to use $ionicLoading inside a factory. (Coz I thought its a good idea to have the code in one place without repeating it in each controller)

angular.module('<my app>.message', ['ionic'])                                                                                                                                  
   .factory('Message', [function($scope, $ionicLoading){                                                                                                                          
     
     var messageFactory = {};                                                                                                                                                     
     
    messageFactory.successMessage = function(message){                                                                                                                           
       $ionicLoading.show({ templateUrl: 'templates/messages/success.html',                                                                                                       
                            noBackdrop: true, duration: 1000 });                                                                                                                  
     }                                                                                                                                                                            
     return messageFactory;                                                                                                                                                       
   }]); 

But the problem is I’m getting this error Cannot read property 'show' of undefined, Since the same code works fine in the controller, I’m wondering if this meant to be working inside a controller. If so , my question is how can I avoid using the same code for message boxes inside a project.

thanks in advance

cheers

sam

You are using the Inline Array Annotation and you forgot to add the dependencies. Another way to solve it is to remove the brackets.

[       '$scope', '$ionicLoading',
function($scope,   $ionicLoading)
{
  ... 
}]

By the way, I don’t think it’s a good idea to access scopes inside factories or services.

Thanks a lot @joseadrian, I’ll try this. I agree with you on using $scope inside the factory. But my requirement is to pass the message variable to html template. do u have a suggestion to archive this

thanks again

cheers,

Sam