AngularJS - 如果一个控制器调用服务或工厂? [英] AngularJS - Should a controller call a service or factory?

查看:129
本文介绍了AngularJS - 如果一个控制器调用服务或工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个角控制器需要调用一些逻辑,也许一样调用Web API,即逻辑可以在角度的服务或工厂封装。 (还有其他的选择,但是这不是一个服务和工厂之间的区别很重要的。)

When an angular controller needs to invoke some logic, like perhaps calling a web API, that logic can be encapsulated in an angular service or factory. (There are other options, but that's not important for the difference between a service and a factory.)

我只是做了一些谷歌搜索,并有一吨的解释什么服务和工厂的文章和视频在那里。这些文章和视频不解释使用哪一个,为什么。所以,如果我写code在我的角度控制器和我想用一个工厂或服务,其中一个我应该使用,为什么?

I just did some googling and there are a ton of articles and videos out there that explain what services and factories are. These articles and videos don't explain which one to use and why. So if I'm writing code in my angular controller and I want to use a factory or service, which one should I use and why?

我不关心他们的工作,这其中至少有一个是单身,一个函数的一个回报和实例,一个是用来使用new关键字,等等...

I don't care about how they work, that at least one of them is a singleton, that one returns and instance of a function, one is used with the new keyword, blah...

两种服务和工厂似乎被使用和调用以相同的方式。因此,有人可以解释这种情况下,我会使用一个比其他?为什么?它甚至关系呢?

Both services and factories appear to be used and called in the same way. So can someone explain in which case I would use one over the other and why? Does it even matter?

**编辑**

建议的可能的回答状态是:

The suggested possible answer states this:

您可以完成与两个同样的事情。然而,在某些情况下
  工厂给你多一点灵活性,以创建一个
  用一个简单的语法可注射的。这是因为当
  myInjectedService必须始终是一个对象,myInjectedFactory可
  一个对象,一个函数引用,或在所有的任意值。

You can accomplish the same thing with both. However, in some cases the factory gives you a little bit more flexibility to create an injectable with a simpler syntax. That's because while myInjectedService must always be an object, myInjectedFactory can be an object, a function reference, or any value at all.

如果仅此而已,那么我的问题是确实的回答。但它会很高兴地知道,如果是这样的话。这是唯一的理由在服务使用工厂?

If that's it, then my question is indeed answered. But it would be nice to know if that's the case. Is that the only reason to use a factory over a service?

推荐答案

使用工厂如果对象定义语法是preferable

Use factory if object definition syntax is preferable

app.factory('factory', function () {
    ...
    return {
        value: '...'
    };
});

或返回的东西是不是出于某种原因的对象。

or it returns something that is not an object for some reason.

使用服务如果这个语法为preferable

Use service if this syntax is preferable

app.service('service', function () {
    this.value = '...';
});

还是应将其从另一个构造,例如返回与创建的对象。

app.factory('factory', function () {
    return new someConstructor();
});

VS

app.service('service', someConstructor);

有一个良好的用例服务是,您可以无缝重构与 controllerAs 语法从共同继承现有的控制器服务,在这种情况下,没有则要求语句替换,<一个href=\"https://stackoverflow.com/questions/31640868/how-angular-controller-and-scope-inheritance-works/31641695#31641695\">as这里图所示:

A good use case for service is that you can seamlessly refactor existing controllers with controllerAs syntax to inherit from common service, in this case no this statements replacement is required, as shown here:

app.service('parentControllerService', function () {
    this.value = '...';
});

app.controller('MainController', function (parentControllerService) {
    angular.extend(this, parentControllerService);
});

app.controller('ChildController', function (parentControllerService) {
    angular.extend(this, parentControllerService);
    this.value = '!!!';
});

这篇关于AngularJS - 如果一个控制器调用服务或工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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