加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular js在预期时不更新dom

发布时间:2020-12-17 08:34:01 所属栏目:安全 来源:网络整理
导读:我有一个小提琴,但基本上它正在做什么地理编码输入到文本框的地址.输入地址并按下“enter”后,dom不会立即更新,而是等待文本框的其他更改.如何在提交后立即更新表格? 我对Angular很新,但我正在学习.我发现它很有趣,但我必须学会以不同的方式思考. 这是小提
我有一个小提琴,但基本上它正在做什么地理编码输入到文本框的地址.输入地址并按下“enter”后,dom不会立即更新,而是等待文本框的其他更改.如何在提交后立即更新表格?
我对Angular很新,但我正在学习.我发现它很有趣,但我必须学会以不同的方式思考.

这是小提琴和我的controller.js

http://jsfiddle.net/fPBAD/

var myApp = angular.module('geo-encode',[]);

function FirstAppCtrl($scope,$http) {
  $scope.locations = [];
  $scope.text = '';
  $scope.nextId = 0;

  var geo = new google.maps.Geocoder();

  $scope.add = function() {
    if (this.text) {

    geo.geocode(
        { address : this.text,region: 'no' 
        },function(results,status){
          var address = results[0].formatted_address;
          var latitude = results[0].geometry.location.hb;
          var longitude = results[0].geometry.location.ib;

          $scope.locations.push({"name":address,id: $scope.nextId++,"coords":{"lat":latitude,"long":longitude}});
    });

      this.text = '';
    }
  }

  $scope.remove = function(index) {
    $scope.locations = $scope.locations.filter(function(location){
      return location.id != index;
    })
  }
}
您的问题是地理编码功能是异步的,因此在AngularJS摘要周期之外更新.你可以通过在$scope.$apply的调用中包装你的回调函数来解决这个问题.这允许AngularJS知道运行摘要,因为东西已经改变了:
geo.geocode(
  { address : this.text,region: 'no' 
  },status) {
    $scope.$apply( function () {
      var address = results[0].formatted_address;
      var latitude = results[0].geometry.location.hb;
      var longitude = results[0].geometry.location.ib;

      $scope.locations.push({
        "name":address,"long":longitude}
      });
    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读