从相同的服务调用AngularJS服务功能? [英] Calling a function in AngularJS service from the same service?

查看:106
本文介绍了从相同的服务调用AngularJS服务功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的方式定义的AngularJS服务

I have an AngularJS service defined in the following way

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

  this.anotherPublicFunction(param) {
    // how to call publicFunction(param)?
    ...
  };
});    

和我想无论是从外服调用第一个函数(由工作正常myService.publicFunction(XXX)),并从另一个功能相同的服务,即 anotherPublicFunction 既不之一 this.publicFunction(参数) myService.publicFunction(参数)将不会从工作第二个函数中,我可以理解。

and I would like to call the first function both from outside the service (which works fine by myService.publicFunction(xxx)) and from another function in the same service, i.e. anotherPublicFunction. Neither one of this.publicFunction(param) or myService.publicFunction(param) won't work from within the second function, and I can understand that.

编辑:

其实整个问题是由东西,你可以用我的例子不是单独繁殖引起的。我在一个单独的控制器通过第二函数作为回调参数到另一个功能,当它被呼叫时,参照不起作用。


Actually the whole problem was caused by something you can't reproduce with my example alone. 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 doesn't work.

例如

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

失败在 anotherPublicFunction ,因为这个不能得到解决。

fails inside anotherPublicFunction because this can't be resolved.

我写了一个Plunker显示问题:
<一href=\"http://plnkr.co/edit/rrRs9xnZTNInDVdapiqF?p=info\">http://plnkr.co/edit/rrRs9xnZTNInDVdapiqF?p=info

(我还是会离开这里的问题的情况下,它会帮助别人。)

我知道我可以通过使用一个参考服务或类似这样的第一个函数解决这个问题。

I know I could get around the problem by using a reference to the service or the first function like this

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

或本

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

但两者看起来脏黑客。

but both seem like dirty hacks.

是否有从第二次是在这种情况下调用的第一个函数的好办法?还是我做一些完全错误摆在首位,以有这样的服务?

我发现这些问题具有很好的答案:

I found these questions with good answers:

  • Angularjs share functions inside service
  • AngularJs call an internal service function from self

,但他们从我的问题不同,因为它们中的一个已经被称为一个单独的,内部功能,这是和另一种是使用一个工厂代替的服务的

but they differ from my problem since one of them has a separate, internal function that was to be called, and the other one was using a factory instead of a service.

编辑:

发布之后我马上意识到我也可以这样做:

After posting this I immediately realized I could also do this:

var actualWork = function(param) {
  ...
}

this.publicFunction(param) {
  actualWork(param);
};

this.anotherPublicFunction(param) {
  actualWork(param);
  ...
};

这似乎并不像其他选项一样糟糕,到目前为止...有没有更好的办法?

which doesn't seem quite as bad as the other options so far... Are there better approaches?

推荐答案

我觉得最好的办法是这样的:

I think the best way is to go like this:

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 if you use this, it may get conflict with the scope where you use the service.

这篇关于从相同的服务调用AngularJS服务功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆