页面加载时的角度对象

时间:2014-07-18 19:37:38

标签: angularjs

我有一个建立在角度上的地图。 https://vineyardcincinnati.com/nigeria-map

我根据点击的google标记进行了字段更新。有没有办法根据页面加载初始化角度对象?在用户基于click事件发送JSON数据之前,内容框当前是空白的。

这是我的角度代码:

$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);

$scope.markers = [];

var infoWindow = new google.maps.InfoWindow({
maxWidth: 200
});

var createMarker = function (info){

    var marker = new google.maps.Marker({
        map: $scope.map,
        position: new google.maps.LatLng(info.lat, info.long),
        title: info.location,
        image: info.img,
        date: info.date,
        quote: info.quote
    });

    marker.content = '<div class="infoWindowContent">' + info.desc + '</div>';

    google.maps.event.addListener(marker, 'click', function(){
        infoWindow.setContent(marker.content);
        $scope.selectedMarker = marker;
        $scope.$apply();
        infoWindow.open($scope.map, marker);
    });

    $scope.markers.push(marker);

}  

for (i = 0; i < cities.length; i++){
    createMarker(cities[i]);
}

$scope.openInfoWindow = function(e, selectedMarker){
    e.preventDefault();
    google.maps.event.trigger(selectedMarker, 'click');
}

});

这是我的观点

<div id="containter" ng-app="mapsApp" ng-controller="MapCtrl">

<div id="map"></div>

<div id="class">
     <h1> {{selectedMarker.title}}</h1>
     <p>{{selectedMarker.quote}}</p>
     <p>Completed {{selectedMarker.date}}</p>
</div>

</div>

2 个答案:

答案 0 :(得分:0)

在指令的控制器中使用$ timeout:

$timeout(function() { 
     // initialize plugin on load here...
});

答案 1 :(得分:0)

您可以使用module.run

angular.module('myModule',[])
.run(function($rootScope){
  $rootScope.selected = "foo";
  // code runs on module init
}