question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Dionescommented, Jan 7, 2016

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))
});
0reactions
amitpatil321commented, Dec 1, 2016

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

angularjs - $watch ngModel not firing - Stack Overflow
I have made a directive to handle file uploads, and it works when uploading files, but not so good when preloading data because...
Read more >
ngModel - AngularJS: API
ngModel watches the model by reference, not value. This is important to know when binding inputs to models that are objects (e.g. ·...
Read more >
Difference between (change) and (ngModelChange) in Angular
Using (ngModelChange) essentially is listening to the input event, and setting the model if a user types, pastes or changes the input's value....
Read more >
I'd like to be able to use ngModel without specifying a name
I think this is a valid case, specially when I don't want to have the form.value to contain the value specified by [(ngModel)]....
Read more >
48 answers on StackOverflow to the most popular Angular ...
We have to import OnInit in order to use like this (actually ... Can't bind to 'ngModel' since it isn't a known property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found