范围值已更改但手表未触发

时间:2016-06-30 14:16:42

标签: angularjs angularjs-directive

我使用指令进行地理位置输入,并返回范围内的坐标值。我想看看范围何时获得价值所以我可以做点什么。

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

    });

1 个答案:

答案 0 :(得分:1)

改为此,

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