angular-google-places-autocomplete
angular-google-places-autocomplete copied to clipboard
i want to use $watch for the ng-model in input, DOESNT WORK
i want to use $watch for the ng-model in input, so that once ng-model gets changed, I can use that to move my map around?
// when autolocation search changes
$scope.$watch('autolocation', function(){
console.log("changed location!");
})
<input type="text" g-places-autocomplete ng-model="autolocation" options="autocompleteOptions" class="map-controls" placeholder="Search Location"/>
try to add watch collection (3rd argument -> true)?
$scope.$watch('autolocation', function(){
console.log("changed location!");
}, true)
try:
controller:
$scope.autolocation = {
model: null
}
$scope.$watch('autolocation.model', function(newValue, oldValue){
console.log(angular.toJson(newValue, true));
console.log("changed location!");
});
html:
<input type="search" g-places-autocomplete ng-model="autolocation.model">
But it will trigger on each change made on search input.
If you want to receive when a new location is selected, you could use:
$scope.$on('g-places-autocomplete:select', function(event, place) {
$log.warn('new location: ' + JSON.stringify(place))
});
How can I capture user typing keywords with $watch? sometime address can't be found, but I still need to catch the input text value..
the above $watch only works if user location is selected.
No you need to watch the input model in my opinion. I know this is an old thread, but then i suppose this repo is mothballed.
Put the watch into the controller which holds your input
app.controller('ctrl1', function($scope, modals, $http, $rootScope) {
$scope.$watch('placeCol', function() {
console.log('form placeCol has been changed!');
console.log($scope.placeCol);
}, true);
}));
Where HTML is:
<div ng-controller="ctrl1"><input class="form-control" g-places-autocomplete ng-model="placeCol"/></div>
That should sort you out.
@Diones Both of your solutions worked great, for the first solution i had to make small correction like below
if(typeof newValue == "object"){
this will prevent trigger on each change made on search input