1

How can I build a service that can be called from everywhere I need with a custom param.

I have this code:

.service('loadingService', function($ionicLoading) {
    this.showLoading=function(textToBeDisplayed){
        $ionicLoading.show({
          noBackdrop: false,
          templateUrl: 'templates/loading-template.html'
        });
    };
    this.hideLoading=function(){
        $ionicLoading.hide();
    };  
});

View: (loading-template.html)

<div>{{textToBeDisplayed}}<ion-spinner icon="ios"/></div>

Controller:

loadingService.showLoading('loading all data');

Loading is displayed but "textToBeDisplayed" is not visible in the view.
Any ideeas ?

Thanks

5
  • 1
    do you want to create a modal? you can use modal Service. Commented Apr 9, 2015 at 16:06
  • 1
    I think you need to set the textToBeDisplayed on that scope being used. I'm not familiar with $ionicLoading but the documentation mentions a scope option... Commented Apr 9, 2015 at 16:11
  • it's working if I move all the code in my controller, define have a $scope.textToBeDisplayed and add "scope=$scope" to $ionicLoading . But I wanted to have something generic..loadingService.showLoading('loading all data'); Commented Apr 9, 2015 at 16:30
  • because I don't want to add everywhere in my app, in all controller all this code Commented Apr 9, 2015 at 16:31
  • you can add it to $rootScope Commented Apr 9, 2015 at 16:31

2 Answers 2

3

Just do this:

.service('loadingService', function($rootScope) {
    return function(textToBeDisplayed) {
        $rootScope.textToBeDisplayed = textToBeDisplayed;
        //put whatever else you want here.
    }
});

call it like this in your controller:

loadingService('someText');

Put this binding in your HTML:

{{textToBeDisplayed}}

Don't forget to inject the service into any controller you need to access it from.

plnkr: http://plnkr.co/edit/n1LdDAG8k9C1JC7qBDyM?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

I'm don't know if is a good practice to add many things on rootScope :) is there any way to clean that var from rootscope when I hide the loading ?
2

I think your service should use template as you are passing template content

.service('loadingService', function($ionicLoading) {
    this.showLoading=function(textToBeDisplayed){
        $ionicLoading.show({
          noBackdrop: false,
          template: '<div>'+textToBeDisplayed+'<ion-spinner icon="ios"/></div>'
        });
    };
    this.hideLoading=function(){
        $ionicLoading.hide();
    };  
});

2 Comments

this is a good solution even if I wanted to have template in a separate html file :) .. until I find a better solution I will use this
This is a better specific solution for this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.