angular.service('myService', myServiceFunction);
angular.factory('myFactory', myFactoryFunction);
I had trouble wrapping my head around this concept until I put it to myself this way:
(我无法绕过这个概念,直到我这样说:)
Service : the function that you write will be new -ed:
(服务 :你编写的函数将是新的 :)
myInjectedService <---- new myServiceFunction()
Factory : the function (constructor) that you write will be invoked :
(Factory :将调用您编写的函数 (构造函数):)
myInjectedFactory <--- myFactoryFunction()
What you do with that is up to you, but there are some useful patterns...
(你用它做什么取决于你,但有一些有用的模式......)
Such as writing a service function to expose a public API: (比如编写一个服务函数来公开一个公共API:)
function myServiceFunction() {
this.awesomeApi = function(optional) {
// calculate some stuff
return awesomeListOfValues;
}
}
---------------------------------------------------------------------------------
// Injected in your controller
$scope.awesome = myInjectedService.awesomeApi();
Or using a factory function to expose a public API: (或使用工厂函数公开公共API:)
function myFactoryFunction() {
var aPrivateVariable = "yay";
function hello() {
return "hello mars " + aPrivateVariable;
}
// expose a public API
return {
hello: hello
};
}
---------------------------------------------------------------------------------
// Injected in your controller
$scope.hello = myInjectedFactory.hello();
Or using a factory function to return a constructor: (或使用工厂函数返回构造函数:)
function myFactoryFunction() {
return function() {
var a = 2;
this.a2 = function() {
return a*2;
};
};
}
---------------------------------------------------------------------------------
// Injected in your controller
var myShinyNewObject = new myInjectedFactory();
$scope.four = myShinyNewObject.a2();
Which one to use?... (哪一个使用?...)
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. (这是因为myInjectedService必须始终是一个对象,myInjectedFactory可以是一个对象,一个函数引用或任何值。)
For example, if you wrote a service to create a constructor (as in the last example above), it would have to be instantiated like so: (例如,如果您编写了一个服务来创建构造函数(如上面的上一个示例所示),则必须实例化它:)
var myShinyNewObject = new myInjectedService.myFunction()
which is arguably less desirable than this:
(这可能比这更不可取:)
var myShinyNewObject = new myInjectedFactory();
(But you should be wary about using this type of pattern in the first place because new -ing objects in your controllers creates hard-to-track dependencies that are difficult to mock for testing. Better to have a service manage a collection of objects for you than use new()
wily-nilly.)
((但是你应该首先考虑使用这种类型的模式,因为你的控制器中的新对象会创建难以模拟测试的难以跟踪的依赖关系。最好让服务管理一组对象你比使用new()
狡猾的。))
One more thing, they are all Singletons... (还有一件事,他们都是单身人士......)
Also keep in mind that in both cases, angular is helping you manage a singleton.
(还要记住,在这两种情况下,angular都可以帮助您管理单身人士。)
Regardless of where or how many times you inject your service or function, you will get the same reference to the same object or function. (无论您注入服务或功能的位置或次数,您都将获得对同一对象或功能的相同引用。)
(With the exception of when a factory simply returns a value like a number or string. In that case, you will always get the same value, but not a reference.) ((除了工厂只返回一个数字或字符串之类的值。在这种情况下,您将始终获得相同的值,但不是引用。))