Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I still can't figure out when you'd use service over the factory.

From what I can tell factory is commonly used to create "common" functions that can be called by multiple Controllers.

The Angular docs seem to prefer factory over service. They even refer to "service" when they use factory which is even more confusing! http://docs.angularjs.org/guide/dev_guide.services.creating_services

So when would one use service?

Is there something that is only possible or much easier done with service?

Is there anything different that goes on behind the scenes? Performance/memory differences?

Here's an example. Other than the method of declaration, they seem identical and I can't figure out why I'd do one vs the other. http://jsfiddle.net/uEpkE/ 

1 Answer

0 votes
by (106k points)

You should use a service when you get an instance of a function ("this" keyword). And a factory you will use when you will get the value that is returned by invoking the function reference(the return statement in a factory).

Related questions

0 votes
1 answer
asked Aug 16, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
+2 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...