0
votes

If I have a method being called within an Angular service how would I be able to call a method within this method? Is it appropriate to create a function inside of a method which resides in a service? Below is an example. The called function modalSuccess does not fire. Also, trying to use $scope or $rootScope fails along with calling the service name followed by a new method such as last.successModal().

angular.module('main').service('last', function($location, $http)
{
    this.submit = function(){
        function modalSuccess(){
           console.log('hello world'); 
        }
        modalSuccess() --> does not fire
}

Example with $rootScope does not work either

angular.module('main').service('last', function($location, $http, $rootScope)
{
    this.submit = function(){
     $rootScope.modalSuccess = function(){
           console.log('hello world'); 
        }
       $rootScope.modalSuccess() --> does not fire
}

Even adding a new method does not work

angular.module('main').service('last', function($location, $http, $rootScope)
{
    this.submit = function(){
     this.modalSuccess = function(){
       console.log('hello world'); 
     }
      last.modalSuccess() --> does not fire, returns an error
}
2

2 Answers

1
votes

first example looks correct except that you did not close the curly braces of the submit function (that should cause an error).

//1° example
angular.module('main').service('last', function($location, $http)
{
    this.submit = function(){
        function modalSuccess(){
           console.log('hello world'); 
        }
        modalSuccess() --> does not fire

   }//you missed this curly braces
}

second example same as above, curly braces not closed

third example should throw an error because the above and because you're trying to access the service inside itself using the keyword reserved to angular for for dependency injection.

//3° example
angular.module('main').service('last', function($location, $http, $rootScope)
{
    this.submit = function(){
     this.modalSuccess = function(){
       console.log('hello world'); 
     }
      last.modalSuccess() //this is not possible, you cannot refer the 
      //service with 'last' keyword here, but use 'this' as you did above

    } //curly braces missing here
}
0
votes

You never assign the function to anything. Also why declare a function in a function, that seems counter intuiative.

angular.module('main').service('last', function($location, $http)
{
    this.submit = function(){
        var modalSuccess = function(){
           console.log('hello world'); 
        }
        modalSuccess(); // <= should now work
    }
}

Or alternatively

angular.module('main').service('last', function($location, $http)
{
    var me = this; // using me as this can change depending on the call context if you are referencing it inside of a method
    me.modalSuccess = function() {
       console.log('hello world'); 
    };

    me.submit = function(){
        me.modalSuccess(); // <= should now work
    }
}