angular-google-places-autocomplete icon indicating copy to clipboard operation
angular-google-places-autocomplete copied to clipboard

i want to use $watch for the ng-model in input, DOESNT WORK

Open fkkcloud opened this issue 8 years ago • 5 comments

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"/>

fkkcloud avatar Sep 23 '15 06:09 fkkcloud

try to add watch collection (3rd argument -> true)?

$scope.$watch('autolocation', function(){
    console.log("changed location!");
}, true)

jeefave avatar Oct 15 '15 02:10 jeefave

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))
});

Diones avatar Jan 07 '16 14:01 Diones

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.

iBasit avatar May 25 '16 11:05 iBasit

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.

TakesTheBiscuit avatar Nov 04 '16 16:11 TakesTheBiscuit

@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

amitpatil321 avatar Dec 01 '16 19:12 amitpatil321