Can you update a $scope variable in a controller from an external source?
For instance, suppose we have the following controller:
app.controller('citySelectCtrl', ['$scope',function($scope){
}]);
And there is a function in the global scope with an event handler. Now, is it possible to change a $scope
variable when that event occurs? Here's our global function:
function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{
componentRestrictions: {'country': 'in'},
types: ['(cities)']
});
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 22.5937, lng: 78.9629},
zoom: 5,
minZoom: 5
});
}
autocomplete.addListener('place_changed', function() {
infowindow.close();
marker.setVisible(false);
var place = autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
----------------------------------------------------------
//UPDATE $scope.city = place here
----------------------------------------------------------
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
});