在相同的服务中但在$ watchcollection内调用AngularJS服务中的函数

Sle*_*nee 1 javascript service angularjs

这是本主题的后续问题: 从同一服务调用AngularJS服务中的函数?

我意识到为时已晚,我应该打开一个新问题,而不是在那里问它(对不起)

我想要执行以下操作:调用服务函数中的函数,并且调用在$ watchCollection内的额外难度.它看起来像这样:

返回{

myFunc1: function(param) {

},

myFunc2: function(scope,param) {

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

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

    }       
}
Run Code Online (Sandbox Code Playgroud)

由于函数(v){你现在在函数中的另一个函数,this不再是服务而是window.

如何在myFunc2 $ watchCollection参数函数中访问此myFunc1?

提前致谢

mus*_*_ut 9

您可以保存this另一个变量(通常称为selfthat)并使用它来访问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 

    }       
}
Run Code Online (Sandbox Code Playgroud)