为什么这个简单的ng-bind和ng-show不起作用?

时间:2016-03-30 00:52:29

标签: javascript angularjs google-maps

我正在尝试显示从Google的Place Service获取的数据。出于某种原因,我可以将对象记录到控制器内的控制台,但是在HTML文件中,指令根本没有填写。因为我没有使用地图,所以我为节点传递了一个div元素。似乎没有在线指南以这种方式显示属性,所以我尝试将对象分配给范围变量并以这种方式显示它。 $scope.location将完全可行的对象打印到控制台屏幕,但ng-bindng-show指令无法生成任何内容。

controllers.js

Controllers.controller("LocationController", ["$routeParams", "$scope", "$location", function($routeParams, $scope, $location) {

    if (!$routeParams.placeId) return $location.path("/");

    var PlaceService;
    $(document).ready(function() {
        PlaceService = new google.maps.places.PlacesService(document.getElementById("locationContainer"));
    });

    PlaceService.getDetails({ placeId: $routeParams.placeId }, function(place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            $scope.location = place;
            return console.log($scope.location);
        }
        return console.log("ERROR: " + status);
    });

}]);

location.html:

<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12">
            <div class="col-xs-4">
                <img ng-src="{{ location.icon }}">
            </div>
            <div class="col-xs-8">
                <p ng-bind="location.name"></p>
            </div>
        </div>
    </div>
</div>
<div id="locationContainer"></div>

控制台输出:  Object { address_components=[9], adr_address="<span class="street-addr...ountry-name">USA</span>", formatted_address="935 W Wisconsin Ave, Milwaukee, WI 53233, USA", more...}

1 个答案:

答案 0 :(得分:0)

这是您的异步​​调用。 取代

if (status == google.maps.places.PlacesServiceStatus.OK) {
        $scope.location = place;
        return console.log($scope.location);
 }

$scope.$apply(function(){
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        $scope.location = place;
        return console.log($scope.location);
    }
}