user572276 user572276 - 5 months ago 12
AngularJS Question

scope value changed but watch did not fire

I use a directive for geolocation input and gives you back the coordinates value in a scope. I want to watch when the scope gets the value so I could do something.

my plnkr

angular.module('app', ['ngAutocomplete'])
.directive('myForm', function() {

return {
restrict: 'E',
templateUrl: 'myForm.html',

};

})
.controller('formCtrl', function ($scope)
{
//watch for details change
$scope.$watch($scope.details, function () {

//alert('details changed')
$scope.formItem.longitude = $scope.details.geometry.location.lng();
$scope.formItem.latitude = $scope.details.geometry.location.lat();

}, true);

});

Answer

Change to this,

$scope.$watch('details', function () {
    //alert('details changed')
    $scope.formItem.longitude = $scope.details.geometry.location.lng();
    $scope.formItem.latitude = $scope.details.geometry.location.lat();
}, true);