我正在使用angular-google-maps指令。我使用ui-boostrap模式显示地图。我在名为mapSelectLocation.html的文件中定义了模态内容。以下是我打开模态的方法:
var modalInstance = $modal.open({
templateUrl: '/path/to/templates/mapSelectLocation.html',
controller: 'mapSelectLocationController'
});
mapSelectLocation.html:
<div class='modal-body'>
<ui-gmap-google-map ng-if="readyForMap" center='map.center' zoom='map.zoom'> </ui-gmap-google-map>
</div>
mapSelectLocationController:
function mapSelectLocationController($scope, $modalInstance) {
$scope.map = { center: { latitude: 39.8433, longitude: -105.1190 }, zoom: 10 };
$scope.readyForMap = true;
}
这会显示弹出窗口并适当地渲染地图。但是,当我关闭模态并重新打开它时,地图无法正确渲染。大多数地图区域都是灰色的,只有一小部分地图在左上角可见。
但是,如果我将mapSelectLocation.html的内容从文件本身移动到同一页面上的角度模板脚本中,它会打开并重新打开,每次都能正确呈现:
<script type="text/ng-template" id="mapSelectLocation.template">
<div class='modal-body'>
<ui-gmap-google-map ng-if="readyForMap" center='map.center' zoom='map.zoom'> </ui-gmap-google-map>
</div>
</script>
当然还要更新templateUrl:
var modalInstance = $modal.open({
templateUrl: 'mapSelectLocation.template',
controller: 'mapSelectLocationController'
});
有人可以告诉我为什么不能将我的模态内容放在单独的文件中吗?我宁愿把它分开,也不要使用角度脚本模板。
答案 0 :(得分:2)
找到解决方案。 Angular-Google-Maps提供uiGmapIsReady服务,以便在ui-map准备就绪时通知您。使用此承诺,您可以传递一个回调函数,该函数允许您访问映射的控件属性,该属性具有可用于刷新映射的refresh()函数。这样可以刷新地图,在后续模态打开时正确呈现。
以下是一个示例解决方案:
var module = angular.module('test', ['ui.bootstrap', 'uiGmapgoogle-maps']);
module.controller('controllerA', ['$scope', '$modal', 'uiGmapIsReady', controllerA]);
module.controller('mapSelectLocationController', ['$scope', '$modalInstance', 'uiGmapIsReady', mapSelectLocationController]);
function controllerA($scope, $modal, uiGmapIsReady) {
$scope.openMapDialog = function () {
var modalInstance = $modal.open({
templateUrl: '/app/mapTemplate.html',
controller: 'mapSelectLocationController'
});
modalInstance.result.then(function (test) {
debugger;
});
}
}
function mapSelectLocationController($scope, $modalInstance, uiGmapIsReady) {
$scope.map = { center: { latitude: 39.8433, longitude: -105.1190 }, zoom: 10 };
$scope.readyForMap = true;
$scope.control = {};
uiGmapIsReady.promise().then(function (maps) {
$scope.control.refresh();
});
}
module.config(function (uiGmapGoogleMapApiProvider) {
uiGmapGoogleMapApiProvider.configure({
v: '3.17',
libraries: 'weather,geometry,visualization'
});
});
模板:
<div class="modal-body">
<h4>Click a location on the map</h4>
<hr />
<div class="row">
<ui-gmap-google-map center='map.center' control='control' zoom='map.zoom'></ui-gmap-google-map>
</div>
<h4>Available Addresses</h4>
<hr />
<div class="row">
<div class="grid no-border">
<div class="grid-body">
Addresses will be listed here...
</div>
</div>
</div>
对于角度谷歌地图v2及以下版本,请参阅此SO答案: How to wait for Angular Google Maps to append getGMap to the control object 基本上从v2开始使用uiGmapIsReady。