2

If I follow this particular practice of making factories:

    myApp.factory('myService', function () {
        var somevalue = 2;

        var myServiceApi = {
            theValue: somevalue,
            updatevalue: updateValue
        }

        return myServiceApi;

        function updateValue(newValue) {
            somevalue = newValue;
        }
    });

Each and every time the service is injected the value of somevalue is always initialized as 2, even though I have updated it earlier with the UpdateValue method. if I however use a getter method on the value it is update in all instances of the service.

http://jsfiddle.net/IngoVals/hd1r1bmp/

What is going on in the background here?

Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
  • Have a look at this: http://stackoverflow.com/questions/16023451/binding-variables-from-service-factory-to-controllers – Jonathan Smith Apr 26 '15 at 17:13
  • @jonnyknowsbest Yeah, I understand variable binding doesn't work, I just didn't get while new instanced singletons didn't get the updated fields. Thanks anyways. – Ingó Vals Apr 27 '15 at 08:46

3 Answers3

5

As you know, factory functions are only called once - angular will return the same object for subsequent usages of the factory in your controllers.

Your fiddle isn't actually testing the value of somevalue - it's testing the value of

myService.theValue

This property will return the value of the private variable somevalue at the time of instantiation, so it will always be "2" in your example. It does not get changed when somevalue changes.

The getter

myService.getvalue()

returns the value of the private variable somevalue at the current time, so it changes as different controllers update the value.

myApp.factory('myService', function () {
    var somevalue = 2;

    var myService = {
        //equivalent to theValue: 2
        theValue: somevalue,
        updatevalue: updateValue,
        getvalue: getValue
    }

    return myService;

    function getValue() {
        return somevalue;
    }

    function updateValue(newValue) {
        somevalue = newValue;
    }
});
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75
  • It's all very obvious now that you mention it, don't understand how I missed this. So if I change the update method to myService.theValue this would work as I thought it would http://jsfiddle.net/IngoVals/hd1r1bmp/3/ – Ingó Vals Apr 27 '15 at 08:43
3

someValue is initialized only once and properly updated when updateValue() is called.

myServiceApi.theValue is set once and never changes.

var somevalue = 2;

var myServiceApi = {
      theValue: somevalue,

means create an object and set the property theValue to whatever value somevalue has, in this case 2. There is no connection between theValue and somevalue. And since you never change theValue it's always 2.

Furthermore there is only one instance of myServiceApi. The same instance is used by all controllers.

a better oliver
  • 26,330
  • 2
  • 58
  • 66
-1

Problem is this var somevalue = 2;

That's a controller variable, I believe you need it to make it a controller $scope variable: $scope.somevalue = 2; That way it's updatable from other Controllers / Services etc.

I like to write $scope variables like this to cut down on characters and also be more readable:

var vs = $scope;
    vs.myVar   = '',
    vs.value   = '',
    vs.myBool  = true,
    vs.myArray = [];

vs.myFunc = function() {
    console.log(vs.value);
};
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529