Calling a function in an AngularJS service from the same service?

I have an AngularJS function defined as follows.

angular.module('myService').service('myService', function() { this.publicFunction(param) { ... }; this.anotherPublicFunction(param) { // how to call publicFunction(param)? ... }; }); 

and I would like to name the first function both from outside the service (which works fine using myService.publicFunction(xxx) ), and from another function in the same service, i.e. anotherPublicFunction . None of this.publicFunction(param) or myService.publicFunction(param) will work from the second function, and I can understand it.




EDIT:
Actually the whole problem was caused by the fact that you cannot reproduce with my example. I passed the second function as a callback parameter to another function in a separate controller, and when it is called, the reference to this does not work.

eg.

 anotherService.someCall('a', 123, myService.anotherPublicFunction); 

crashes inside anotherPublicFunction because this cannot be resolved.

I wrote Plunker to show the problem: http://plnkr.co/edit/rrRs9xnZTNInDVdapiqF?p=info

(I will still leave the question here if this helps someone else.)




I know that I can work around the problem using a service link or a first function like this

 var ms = this; this.anotherPublicFunction(param) { ms.publicFunction(param); ... }; 

or

 var pf = this.publicFunction; this.anotherPublicFunction(param) { pf(param); ... }; 

but both look like dirty hacks.

Is there a good way to call the first function from the second in this case? Or am I doing something completely wrong, in the first place, to have such a service?

I found these questions with good answers:

  • Angularjs Access Functions Inside a Service
  • AngularJs calls an internal service function from itself

but they are different from my problem, because one of them has a separate internal function that should have been called, and the other used a factory instead of a service.

EDIT:

After publishing this, I immediately realized that I could do this:

 var actualWork = function(param) { ... } this.publicFunction(param) { actualWork(param); }; this.anotherPublicFunction(param) { actualWork(param); ... }; 

that so far is not as bad as other options so far ... Are there any better approaches?

+63
angularjs
Nov 07 '13 at 17:30
source share
11 answers

I think the best way is:

 var myFunctions = { myFunc1: function(param) { }, myFunc2: function(param) { return foo + myFunctions.myFunc1(param)// do some stuff with the first function } } return myFunctions; 

because I think that if you use this, it may conflict with the scope of your use of the service.

+38
Feb 28 '14 at 8:02
source share

I ran into this problem in my own angular code and decided to use this solution form:

 angular.module('myService').service('myService', function() { var myService = this; myService.publicFunction(param) { ... }; myService.anotherPublicFunction(param) { myService.publicFunction(param); ... }; }); 

I preferred this approach because in my code I use publicFunction, and inside anotherPublicFunction I iterate over several parameters that need to be called publicFunction as part of the process.

+29
Jun 29 '15 at 17:58
source share

Sounds good, but what do you do when this no longer = service?

This is in the following example:

 return { myFunc1: function(param) { }, myFunc2: function(param) { scope.$watchCollection(param,function(v) { return foo + this.myFunc1(param)// do some stuff with the first function } } } 

Due to function(v) { now you are in another function inside the function, and this is no longer a service, but a window.

+6
Nov 18 '13 at 16:22
source share

You can simply return an object from your service, for example

 return { myFunc1: function(param) { }, myFunc2: function(param) { return foo + this.myFunc1(param)// do some stuff with the first function } } 

This way you access it from the outside with service.myFunc1 and from the inside of your internal functions with that.

+4
Nov 07 '13 at 18:33
source share

You can set 'this' as a variable

 angular.module('myService').service('myService', function() { var that = this; this.publicFunction(param) { ... }; this.anotherPublicFunction(param) { that.publicFunction(param); }; }); 
+3
Oct 27 '15 at 16:55
source share

here is how i implemented:

 .service('testSvc', function () { return { f1: function (v1) { alert('f1 > v1=' + v1); }, f2: function (v2) { alert('f2 > v2=' + v2); this.f1('az'); //use 'this' } } 

})

0
Jun 04 '16 at 12:18
source share

The following helped me: -

  angular.module('myService').service('myService', function() { var self= { this.publicFunction(param) { ... }; this.anotherPublicFunction(param) { // You can call the publicfunction like below self.publicFunction(param); ... }; return self; } ]);` 
0
Jul 05 '16 at 21:02
source share
 angular.module('myService').service('myService', function() { return { myFunc2: function(param) { return foo + myFunc1(param); } }; function myFunc1(param){ ... }; }); 
0
Nov 24 '16 at 6:58
source share

Well, just create two more pointers / function variables for your service functions with local variables in the service and use them to call service functions from one service:

*

 angular.module('myService').service('myService', function() { **var ref1, ref2;** this.publicFunction = **ref1** = function(param) { ... }; this.anotherPublicFunction = **ref2** = function(param) { // how to call publicFunction(param)? **ref1(argument);** ... }; }); 
0
Jul 20 '17 at 21:18
source share

The best and easiest way to call:

 myapp.service("SomeService", function ($http) { this.func1 = function (params) { // Some thing the service returns return something; } this.func2 = function (params) { // Some thing the service returns var resp = this.func1(params); return something; } } 
-one
Mar 15 '17 at 2:29
source share

If you want to use a factory declaration instead of a service, you can: -

 angular.module('myService').factory('myService', function() { var myService = {}; myService.publicFunction = function(param) { ... }; myService.anotherPublicFunction = function(param) { // call publicFunction(param) like so myService.publicFunction(param); }; return myService; }); 
-one
May 05 '17 at 15:56
source share



All Articles