ng模型数据不能正常工作

ng-model data bing not working properly

本文关键字:工作 常工作 模型 数据 不能 ng      更新时间:2023-09-26

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

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

我的代码中缺少的部分是什么?

下面是JsFiddle代码示例

你错过了一个适用范围()。美元();

见http://fiddle.jshell.net/yfLek2cw/

你的Angular CDNLink在你原来的小提琴里也不能工作。原因是,geolocation回调不是从angular中完成的。因此,脏检查机制在那里不起作用。要再次启动脏检查,必须调用$scope.$apply();

这对于所有外部回调函数都是必需的,即使这些回调函数在角作用域内。Auto $apply只适用于angular的回调函数,比如$timeout或$q service

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

http://fiddle.jshell.net/7v2dfLsv/3/