Angular Code没有出现在html页面上

时间:2015-08-13 01:46:36

标签: javascript html angularjs scope angularjs-scope

我一直在弄乱这个2个小时,找不到解决方案。 下面是显示日志的chrome dev工具输出。

(按顺序)

3 - 未定义

1 - 检查

2 - 谷歌用户

为什么我的html中的地方变量没有更新为“Google用户”?

{
"query" : {
    "filtered" : {
        "query" : {
            "has_child" : {
            "type" : "blog_tag",
            "query" : {
                "filtered" : {
                    "query" : {
                        "term" : {
                            "tag" : "something"
                        }
                    }
                }
            }
        }
    }
},
"aggs" : {
    "my_children" : {
        "children" : {
            "type" : "my_child_type"
        },
        "aggs" : {
            "field_name" : {
                "terms" : {
                    "field" : { "blog.blog_tag.field_name" }
                }
            }
        }
    }
}     

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

myApp.controller('AppCtrl', ['$scope', '$http', '$log',
  function($scope, $http, $log) {

    var request = {
      placeId: 'ChIJX3piIfJ4j4ARXuQlqgn6LaA' //Walmart HQ
    };

    service = new google.maps.places.PlacesService(map);

    service.getDetails(request, function(place, status) {
      $log.log('1 - check');
      $log.log('2 - ' + place.reviews[0].author_name);
      $scope.place = place.reviews[0].author_name;
   });

    $log.log('3 - ' + $scope.place);
  }
]);

1 个答案:

答案 0 :(得分:1)

google.maps.places.PlacesService不是内置的异步处理程序,您需要在异步调用后运行$scope.$apply。使用$scope.$apply更新视图:

$scope.$apply(function () {
    $scope.place = place.reviews[0].author_name;
});

更新1

检查ES6 spec description

  

$ apply()用于从角度框架外部以角度执行表达式。 (例如,来自浏览器DOM事件,setTimeout,XHR或第三方库)。因为我们正在调用角度框架,所以我们需要执行异常处理的适当范围生命周期,执行监视。

可能的更新发生时,Angular执行脏检查。 XHR呼叫不在该范围内。 Angular不会更新视图,除非您要求通过$apply进行脏检查。

最佳做法是:尽可能使用Angular内置提供程序,例如$timeout$http。或ng-clickng-change等。这些将隐式调用$apply