我正在使用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'
});
有人可以告诉我为什么将我的模式内容放在单独的文件中不起作用吗?我宁愿将其分开,也不必使用角度脚本模板。
找到了解决方案。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及以下版本,请参见以下答案:如何等待角度谷歌地图将getGMap附加到控件对象基本上从v2开始,使用uiGmapIsReady。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句