-2

For last 1 day, I am trying to get clear understanding:-

  • What is Service in angular
  • Different ways of creating service
  • Service vs Factory

Posted a question as well, which was marked duplicate & closed.
When should I use factory or service in angular?

But I am surprised to see that, on the angularjs official site,
https://docs.angularjs.org/guide/services
they have mentioned only

factory('factoryName', ['$window', function(win) {}])

Can someone please tell me why so?

Is Factory only the recommended way of creating service by Angular?

Community
  • 1
  • 1
aarti
  • 104
  • 1
  • 6
  • [AngularJS: Service vs provider vs factory](http://stackoverflow.com/questions/15666048/angularjs-service-vs-provider-vs-factory) – Satpal May 17 '16 at 11:06
  • @Satpal, I have read all that, my question is why on the official site factory is only mentioned not any other method of creating service – aarti May 17 '16 at 11:07
  • The *guide* (a.k.a. tutorial) focuses on one, the actual API documentation mentions them all: https://docs.angularjs.org/api/ng/type/angular.Module – deceze May 17 '16 at 11:09

1 Answers1

-1
  1. Factory is the better way to create service in angular since to instantiate Service, angular calls factory or we can say Service is written on the top of Factory.
  2. Factory can also be used to return Value or Class which can be further instantiated

But putting in the mind that moving from angular1.x to angular2.x, we should use Service since they are the function constructor which we will also be using in angular2.x

Suneet Bansal
  • 2,664
  • 1
  • 14
  • 18