Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng模型数据不能正常工作_Javascript_Angularjs_Angularjs Ng Model - Fatal编程技术网

Javascript ng模型数据不能正常工作

Javascript ng模型数据不能正常工作,javascript,angularjs,angularjs-ng-model,Javascript,Angularjs,Angularjs Ng Model,我试图用一个新的地理代码值更新一个字段,但即使模型被更新,它也不会与第一次调用绑定。我的控制器代码如下: function TheReportCtrl($scope) { $scope.code = []; $scope.code.lat = "19.2555500"; $scope.getCode = function () { var geocoder = new google.maps.Geocoder(); var address

我试图用一个新的地理代码值更新一个字段,但即使模型被更新,它也不会与第一次调用绑定。我的控制器代码如下:

function TheReportCtrl($scope) {
    $scope.code = [];
    $scope.code.lat = "19.2555500";
    $scope.getCode = function () {
        var geocoder = new google.maps.Geocoder();
        var address = "12323 Barker Cypress Rd, Cypress, Texas"
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                $scope.code.lat = results[0].geometry.location.k;
            } else {
                console.log("Geocoding failed: " + status);
            }
        });
    }
} 
我的代码缺少的部分是什么


您的代码实际上正在工作,您只需要使用
$apply()
方法更新
$scope
。这是一把小提琴:

您错过了一个 $scope().$apply()

另外,你原来的小提琴里有棱角的CDNLink也不起作用。原因是,地理位置回调不是从角度进行的。因此,脏检查机制在那里不起作用。要再次启动脏检查,必须调用$scope.$apply()


所有外部回调函数都需要这样做,即使这些回调在角度范围内。Auto$apply仅适用于角度回调,如$timeout或$q服务

您需要调用
$scope.$apply()
$scope.code.lat=结果[0]之后。AngularJS不知道您改变了范围。谢谢Sergiu Paraschiv,很遗憾,您的答案在评论中。:)将$scope.apply()更改为$scope.$apply(),它就可以工作了。谢谢,修复了输入错误。