1

this is a follow up question to this topic: Calling a function in AngularJS service from the same service?

I realized too late I should open a new question instead asking it there (sorry for that)

I want to do the following: calling a function within a service function with the additional difficulty the call is within $watchCollection. It looks something like this:

return {

myFunc1: function(param) {

},

myFunc2: function(scope,param) {

    scope.$watchCollection(param,function(v) {

      return foo + this.myFunc1(param)// do some stuff with the first function 

    }       
}

} Because of function(v) { you are now in another function within the function and this is no longer the service but the window.

how can I get access to this myFunc1 within the myFunc2 $watchCollection parameter function?

thanks in advance

1 Answer 1

9

You can save the this in another variable (usually called self or that) and use it to access myFunc1.

myFunc1: function(param) {

},

myFunc2: function(scope,param) {

    var self = this;
    scope.$watchCollection(param,function(v) {

      return foo + self.myFunc1(param)// do some stuff with the first function 

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

Comments

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.