0

I have been experimenting with AngularJS values, and wish to store a global value for accessing and setting in different controllers.

So I have been trying out with the value approach like so:

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

app.value('globalValue', 0);

app.controller('myCtrl', ['$scope', '$rootScope', 'globalValue', function($scope, $rootScope, globalValue) {
    $scope.updateValue = function() {
        globalValue++;
    };    
}]);

app.controller('myCtrlB', ['$scope', '$rootScope', 'globalValue', function($scope, $rootScope, globalValue) {
        $scope.someValueB=globalValue;

}]);

Here's a fiddle

This is not working as I thought it might, so in my fiddle, when clicking the button to increment my 'global', the scope property in myCtrlB does not change.

I have clearly gone about this the wrong way, have I totally misunderstood how to use value()'s here?

Thanks

2
  • Have you tried using angular.constant()? Commented Apr 22, 2015 at 20:15
  • 1
    @user1518802 constant would work as well though it's a bit of a misnomer since the value doesn't remain constant over time. In functional terms it would work fine though since constant is also just a provider with less restrictions than .value docs.angularjs.org/guide/providers The problem is still the same though because of the value being assigned rather than a reference. Commented Apr 22, 2015 at 20:17

1 Answer 1

2

This code should work basically you need an object so both controllers are pointing at the same object and some property of that object is changed. Otherwise you are assigning the initial value of globalValue to some local variable but it's not a reference.

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

app.value('globalValue', {counter:0});

app.controller('myCtrl', ['$scope', '$rootScope', 'globalValue', function($scope, $rootScope, globalValue) {
    $scope.updateValue = function() {
        globalValue.counter++;
    };    
}]);

app.controller('myCtrlB', ['$scope', '$rootScope', 'globalValue', function($scope, $rootScope, globalValue) {
        $scope.someValueB=globalValue;

}]);

Updated fiddle: http://jsfiddle.net/kfxy5hs1/3/

Sign up to request clarification or add additional context in comments.

3 Comments

Ok so how does $scope.someValueB in myCtrB resolve to globalValue.counter? I was expecting that line to read $scope.someValueB=globalValue.counter; What if there were multiple properties set in that globalValue object in the app.value() call?
@mindparse in the view that uses myCtrlB I just updated the binding to point to the specific property of the object defined with the provider/value definition. Any time that you assign a string or a primitive (anything that's not an object) you are really making a copy of the value. In JS if you assign an object you are setting a new pointer to point to the same object, so when you change properties of that object both pointers are pointing at the same object.
Doh I overlooked {{someValueB.counter}} in your version of the view! Makes sense now, thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.