Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about Service vs Factory

Tags:

angularjs

As I understand it, when inside a factory I return an object that gets injected into a controller. When inside a service I am dealing with the object using this and not returning anything.

I was under the assumption that a service was always a singleton, and that a new factory object gets injected in every controller. However, as it turns out, a factory object is a singleton too?

Example code to demonstrate:

var factories = angular.module('app.factories', []); var app = angular.module('app',  ['ngResource', 'app.factories']);  factories.factory('User', function () {   return {     first: 'John',     last: 'Doe'   }; });  app.controller('ACtrl', function($scope, User) {   $scope.user = User; });  app.controller('BCtrl', function($scope, User) {   $scope.user = User; }); 

When changing user.first in ACtrl it turns out that user.first in BCtrl is also changed, e.g. User is a singleton?

My assumption was that a new instance was injected in a controller with a factory?

like image 291
JvdBerg Avatar asked Dec 07 '12 11:12

JvdBerg


People also ask

What is the difference between factory and service?

factory() is a method that takes a name and function that are injected in the same way as in service. The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not.

What is a service in AngularJS?

In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application. AngularJS has about 30 built-in services. One of them is the $location service.

What is the use of $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application.

How does scope inheritance work in AngularJS?

Scope InheritanceIf we define nested controllers, then the child controller inherits the scope of its parent controller. We assign values to the models in shapeController. We override message in child controller named circleController.


1 Answers

All angular services are singletons:

Docs (see Services as singletons): https://docs.angularjs.org/guide/services

Lastly, it is important to realize that all Angular services are application singletons. This means that there is only one instance of a given service per injector.

Basically the difference between the service and factory is as follows:

app.service('myService', function() {    // service is just a constructor function   // that will be called with 'new'    this.sayHello = function(name) {      return "Hi " + name + "!";   }; });  app.factory('myFactory', function() {    // factory returns an object   // you can run some code before    return {     sayHello : function(name) {       return "Hi " + name + "!";     }   } }); 

Check out this presentation about $provide: http://slides.wesalvaro.com/20121113/#/

Those slides were used in one of the AngularJs meetups: http://blog.angularjs.org/2012/11/more-angularjs-meetup-videos.html

like image 123
matys84pl Avatar answered Sep 18 '22 19:09

matys84pl