Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Global variable in view using angular

I have an app with a global variable(actual global variable, not $rootScope). I need to print it to the view using the {{ }} expression. How can I associate a $scope variable of current controller to thisglobal variable, such that I always have the latest value of this global variable printed on screen.

EDIT: Code:

app.controller('placesCtrl', ['$scope','$rootScope',function($scope, $rootScope){
    $scope.place = place;
}]);

var autocomplete,map,place = {};
place.name = "asdf";
function initAutocomplete() {
    ...

    //Initialize google maps autocomplete
    //Add event listener

    autocomplete.addListener('place_changed', function() {

    //update place            
        place = autocomplete.getPlace();
        console.log(place);

        if (!place.geometry) {
            window.alert("No such city found!");
            return;
        }

      });
}
like image 393
ayushgp Avatar asked Feb 20 '26 05:02

ayushgp


1 Answers

If myGlobal is an object, a simple reference to it is enough:

$rootScope.myGlobal = myGlobal;

Now if it's a raw value such as a string or number (or if you change the reference of your object), you could use a closure on it:

$rootScope.getMyGlobal = function() { return myGlobal; }

And then in your HTML write:

<div>{{getMyGlobal()}}</div>
like image 60
floribon Avatar answered Feb 22 '26 17:02

floribon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!