I've been doing a lot of work on Angular.js
and overall I find it to be an interesting and powerful framework.
I know there have been a lot of discussions on Services vs. Factories vs. Providers vs. Values, but I am still pretty confused about what a Factory
is.
Factory has been defined in other StackOverflow discussions as the following:
Factories
Syntax: module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.
I find this explanation to be very difficult to grasp and it doesn't increase my understanding of what a factory is.
Would anyone have any explanations or real life examples to share about what exactly a Factory
is and why you should use it in lieu of a Service
, Provider
, or other?
Update
A service
holds a reference to any object.
A factory
is a function which returns any object
A provider
is a function which returns any function
-phew-
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…