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;
}
});
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With