3

I have a factory House

'use strict';

myApp
.factory('House', [ '$http', '$location', '$rootScope', function($http, $location, $rootScope){

    var numberOfDoors;

    return {

        loadHouse: function(){
           numberOfDoors = 1;
        },

        numberOfDoors: numberOfDoors


    };
}]);

And I have a controller Owner

myApp
.controller('OwnerCtrl', ['$rootScope', '$scope', '$location', 'House', function($rootScope, $scope, $location, House) {


    $scope.buildMe = function() {

        //use value from house
        var sayHelloToDoors = House.numberOfDoors;

    };
}]);

If I run a function House.loadHouse() in some other controller in order to set the variable numberOfDoors - I do set it, but when I switch to a page to reuse that number, I get a undefined message in Owner controller.

Why the value is cleared? It seems like another instance of House is transfered. Could anybody help?

2 Answers 2

8

This is because you're assigning the value of private numberOfDoors to the field numberOfDoors in the factory.

// This following snippet ...

myApp.factory('house', function() {
  var numberOfDoors;

  return {
    numberOfDoors: numberOfDoors
  };
});

// ... will actually become

myApp.factory('house', function() {
  var numberOfDoors; // === undefined

  return {
    numberOfDoors: undefined
  };
});

Even if you would assign the private numberOfDoors an initial value, it would not behave the way you want it to.

myApp.factory('house', function() {
  var numberOfDoors = 123;

  return {
    loadHouse: function() {
      numberOfDoors = 1; // will change the private variable
                         // not the exposed object variable
    },
    numberOfDoors: numberOfDoors // will always be 123
  };
});

Easiest way to make this work is doing something like this:

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

  return {
    reset: function() {
      this.numberOfDoors = 0;
    }, 
    numberOfDoors: 0
  };

});

You can play with in action at this plunker.

Edit:

To encapsulate the number of doors you could do something like this:

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

  var _numberOfDoors = 0;

  return {
    reset: function() {
      _numberOfDoors = 0;
    }, 
    numberOfDoors: function(value) {

      if(value!==undefined) {
        _numberOfDoors = value;
      }

      return _numberOfDoors;
    }
  };

});

Now from your controller you can:

var count = house.numberOfDoors(); // Get the numberOfDoors
house.numberOfDoors(++count);      // Set the numberOfDoors
6
  • Thank you so much for such detailed answer and the plunker, you really made an effort. What I don't understand here is that why it wouldn't be possible to return a private variable but via public method. The thing is that I am doing such in my Authentication factory, and the currentUser variable stays visible, but here numberOfDoors doesn't :/
    – Aleks
    Jan 8, 2014 at 21:46
  • Actually you can return a private variable by a public method. But you have to define the function returning the variable to the object field and not the variable itself. I'll update the answer to clarify.
    – null
    Jan 8, 2014 at 21:59
  • Oh and I'm guessing the currentUser variable you're referring to is an object type, not a value type. Doing something like { user: { name: 'john' } } will expose a reference to the object { name: 'john' }. So changes to the property name are reflected everywhere.
    – null
    Jan 8, 2014 at 22:11
  • @Aleks I guess you're not hoping for a better answer.
    – Stewie
    Jan 8, 2014 at 22:16
  • @null you are guessing good :) I have used exactly that. The answer looks perfect. I will tru it tomorrow and I will let you know how did I go :) +1 now and a solved tomorrow ;)
    – Aleks
    Jan 8, 2014 at 22:33
0

You'll have to change "numberOfDoors: numberOfDoors" to "numberOfDoors: function(){return numberOfDoors;}"

Please see this simple: (available in plnkr)

var service = (function(){
  var privateVar = 0;
  return{
    updateVar: function(val){
      privateVar = val;
    },
    getVar2: privateVar,
    getVar: function(){
      return privateVar;
    }

  }
})();

var instanceA = (function(){
  service.updateVar(1);
  alert(service.getVar());

})();


var instanceB = (function(){
  alert(service.getVar());
  alert(service.getVar2);

})();
1
  • Thanks. I will try both answers tomorrow as it quite late here. But I will return tomorrow
    – Aleks
    Jan 8, 2014 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.