javascript - ng-model 数据 bing 无法正常工作

标签 javascript angularjs angularjs-ng-model

我正在尝试使用新的地理代码值更新字段,但即使模型已更新,它也不会与第一次调用绑定(bind)。我的 Controller 代码如下:

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

我的代码缺少什么部分?

Here is the JsFiddle Code Sample

最佳答案

你错过了一个 $scope().$apply();

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

此外,你原来的 fiddle 中的 Angular CDNLink 也不起作用。原因是,地理位置回调不是从 Angular 完成的。因此脏检查机制在那里不起作用。要再次启动脏检查,您必须调用 $scope.$apply();

这是所有外部回调函数所必需的,即使这些回调位于 Angular 范围内。 Auto $apply 仅适用于 $timeout 或 $q 服务等 Angular 回调

关于javascript - ng-model 数据 bing 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429225/

相关文章:

asp.net - 如何保护我的 AngularJS 和 Web Api 应用程序

angularjs - 使用带有 Angular xeditable 的 bootstrap-datepicker

Angular 6 使用两个共享模块 : Component is not a known element:

javascript - 使用 ng-model 绑定(bind)时保留自定义对象类型

javascript - 使用 DataTable 插件获取具有特定类的表的所有 td 值

javascript - 过滤掉对象中未定义的属性

javascript - for 循环中的 HTML 表 TD 未正确生成

json - 具有 Spring Data Elasticsearch 的查询生成器

javascript - 在 Highcharts 中对图例进行分组

javascript - Angular 中事件监听的位置