0

i am new in angular. so trying to know how to share data between two controller and search google. i visited few pages and found most of the time people use factory to share data. i just like to know can't we do it by service instead of factory ?

1st example

<div ng-controller="FirstCtrl">
  <input type="text" ng-model="data.firstName">
  <br>Input is : <strong>{{data.firstName}}</strong>
</div>
<hr>
<div ng-controller="SecondCtrl">
  Input should also be here: {{data.firstName}}
</div>

myApp.factory('MyService', function(){
  return {
    data: {
      firstName: '',
      lastName: ''
    },
    update: function(first, last) {
      // Improve this method as needed
      this.data.firstName = first;
      this.data.lastName = last;
    }
  };
});

// Your controller can use the service's update method
myApp.controller('SecondCtrl', function($scope, MyService){
   $scope.data = MyService.data;

   $scope.updateData = function(first, last) {
     MyService.update(first, last);
   }
});

2nd example

var myApp = angular.module('myApp', []);

myApp.factory('Data', function(){

    var service = {
        FirstName: '',
        setFirstName: function(name) {
            // this is the trick to sync the data
            // so no need for a $watch function
            // call this from anywhere when you need to update FirstName
            angular.copy(name, service.FirstName); 
        }
    };
    return service;
});


// Step 1 Controller
myApp.controller('FirstCtrl', function( $scope, Data ){

});

// Step 2 Controller
myApp.controller('SecondCtrl', function( $scope, Data ){
    $scope.FirstName = Data.FirstName;
});

examples are taken from this url Share data between AngularJS controllers

please guide me.

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • I recommend you to read this simple and clear article about Angular services vs factories. https://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html – Striped Jun 09 '17 at 14:34
  • your explanation is good but hence i am new in so still not understand why some one will write factory to share data.....why not service ? if possible try to explain it to me more easy way. thanks – Monojit Sarkar Jun 12 '17 at 11:37

1 Answers1

2

Both .service() and .factory() are both singletons as you’ll only get one instance of each Service regardless of what API created it.

Remember that .service() is just a Constructor, it’s called with new, whereas .factory() is just a function that returns a value.

Using .factory() gives us much more power and flexibility, whereas a .service() is essentially the “end result” of a .factory() call. The .service() gives us the returned value by calling new on the function, which can be limiting, whereas a .factory() is one-step before this compile process as we get to choose which pattern to implement and return.

Raaj Dubey
  • 172
  • 1
  • 14
  • you said :- how to prove `Both .service() and .factory() are both singletons` ? is there any way to prove it just by writing and running a program ? – Monojit Sarkar Jun 12 '17 at 11:25
  • your explanation is good but hence i am new in so still not understand why some one will write factory to share data.....why not service ? if possible try to explain it to me more easy way. thanks – Monojit Sarkar Jun 12 '17 at 11:37
  • Factory is a provider with only a $get method, essentially. – Raaj Dubey Jun 12 '17 at 12:33
  • service is a singleton. Good for cross app/controller communication. – Raaj Dubey Jun 12 '17 at 12:34
  • can you give me any code sample which will tell me that service is singleton. – Monojit Sarkar Jun 12 '17 at 12:35
  • factory is also singleton like service ? – Monojit Sarkar Jun 12 '17 at 12:35
  • https://codepen.io/rajMrPerfect/pen/mwPYXQ to example that service is singleton – Raaj Dubey Jun 12 '17 at 12:40
  • A singleton pattern ensures that you always get back the same instance of whatever type you are retrieving, whereas the factory pattern generally gives you a different instance of each type. The purpose of the singleton is where you want all calls to go through the same instance. An example of this might be a class that manages a disk cache, or gets data from a static dictionary; wherever it is important only one known instance interacts with the resource. This does make it less scalable. – Raaj Dubey Jun 12 '17 at 12:43
  • The purpose of the factory is to create and return new instances. Often, these won't actually be the same type at all, but they will be implementations of the same base class. However, there may be many instances of each type, – Raaj Dubey Jun 12 '17 at 12:43
  • tell me what your codepen example is about ? does it show that service is singleton? – Monojit Sarkar Jun 12 '17 at 13:29