我使用NgMap
在我的Angular应用程序中的模态内生成一个地图。使用控制器中的$modal
-injector生成模态,并使用模板视图构建html代码。
第一次模态显示它完美无缺。虽然,第二次地图完全搞砸了。 显示地图的位数,其余部分用浅灰色覆盖。
这里是初始化模态的原因
$modal.open({
templateUrl: 'views/partials/modals/storeShowModal.html',
controller: 'ModalShowStore',
resolve: {
store: function() {
return store;
}
}
});
这是模态控制器
appControllers.controller('ModalShowStore', ['$scope', '$modalInstance', 'store', 'NgMap', function ($scope, $modalInstance, store, NgMap) {
$scope.center = store.latitude+","+store.longitude;
$scope.position = store.latitude+","+store.longitude;
NgMap.getMap().then(function(map) {});
$scope.store = store;
$scope.cancel = function () {
$modalInstance.close();
};
}]);
这是模板html
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" data-ng-click="cancel()" aria-hidden="true">×</button>
<h4 class="modal-title text-align-center fw-bold mt" id="myModalLabel18">{{ store.name }}</h4>
</div>
<div class="modal-body bg-gray-lighter">
<div class="row">
<div class="col-md-12">
<ng-map center="{{ center }}" zoom="15">
<marker position="{{ position }}"></marker>
</ng-map>
</div>
</div>
</div>
任何想法,不管怎么说都非常受欢迎。我想这与动态加载html有关。 感谢名单。
修改
根据要求,这是我非常大的bower.json
{
"name": "sing",
"version": "1.1.1",
"description": "Sing Dashboard App",
"main": "index.html",
"authors": [
"okendoken flatlogic.com"
],
"dependencies": {
"jquery": "~2.1.3",
"jquery-pjax": "~1.9.4",
"bootstrap-sass": "3.3.4",
"font-awesome": "~4.3.0",
"bootstrap-select": "~1.6.3",
"select2": "~3.5.2",
"select2-bootstrap-css": "~1.4.6",
"jasny-bootstrap": "~3.1.3",
"slimScroll": "~1.3.3",
"widgster": "~0.0.2",
"pace.js": "git://github.com/HubSpot/pace.git#v0.7.1",
"animate.css": "~3.2.0",
"jquery-touchswipe": "~1.6.8",
"awesome-bootstrap-checkbox": "~0.3.3",
"angular": "1.6.3",
"angular-ui-router": "~0.2.13",
"ngstorage": "~0.3.0",
"angular-ui-utils": "bower-event",
"angular-bootstrap": "2.5.0",
"angular-animate": "1.6.3",
"angular-bootstrap-select": "~0.0.5",
"angular-datatables": "0.4.2",
"angular-resource": "~1.3.14",
"angular-ui-calendar": "~0.8.1",
"backbone": "~1.1.2",
"backbone.paginator": "~2.0.2",
"backgrid": "~0.3.5",
"backgrid-paginator": "03632df8ad238e3d043c0fd471a6c78494e1bdfc",
"jquery.sparkline": "http://omnipotent.net/jquery.sparkline/2.1.2/jquery.sparkline.min.js",
"d3": "~3.5.4",
"rickshaw": "~1.5.1",
"nvd3": "~1.7.1",
"morris.js": "~0.5.1",
"flot": "~0.8.3",
"flot.animator": "https://github.com/Codicode/flotanimator.git#3c256c0183d713fd3bf41d04417873928eb1a751",
"flot-orderBars": "https://github.com/emmerich/flot-orderBars.git#d44ff4f2fec4c8bb1254e59567ee39fa3684372e",
"jquery-autosize": "~1.18.17",
"bootstrap3-wysihtml5": "git://github.com/schnawel007/bootstrap3-wysihtml5.git#5f07036cb228c1cc8bab2d271701ceff0de66828",
"switchery": "~0.7.0",
"eonasdan-bootstrap-datetimepicker": "~3.1.3",
"mjolnic-bootstrap-colorpicker": "~2.0.1",
"holderjs": "~2.5.0",
"dropzone": "~4.0.0",
"jQuery-Mapael": "https://github.com/neveldo/jQuery-Mapael.git#v0.7.1",
"gmaps": "~0.4.16",
"markdown": "~0.5.0",
"to-markdown": "~0.0.3",
"bootstrap-markdown": "~2.8.0",
"seiyria-bootstrap-slider": "~4.4.2",
"bootstrap_calendar": "~1.0.1",
"jvectormap": "http://jvectormap.com/binary/jquery-jvectormap-1.2.2.zip",
"jvectormap-world": "http://jvectormap.com/js/jquery-jvectormap-world-mill-en.js",
"parsleyjs": "~2.0.7",
"twitter-bootstrap-wizard": "git://github.com/VinceG/twitter-bootstrap-wizard.git#62a9aca8cc61fdb496c4bf7b92e1ff70c698667e",
"bootstrap-application-wizard": "git://github.com/amoffat/bootstrap-application-wizard.git#3768da3142f43428c5f63284407a2481a9e283d3",
"jquery-ui": "~1.11.2",
"jquery-ui-touch-punch": "git://github.com/furf/jquery-ui-touch-punch.git#4bc009145202d9c7483ba85f3a236a8f3470354d",
"datatables": "~1.10.4",
"jquery.nestable": "git://github.com/dbushell/Nestable.git#4f93032cfafe2002f24ed6bd7dc1510931498503",
"messenger": "~1.4.1",
"jquery.easy-pie-chart": "~2.1.6",
"fullcalendar": "~1.6.7",
"shufflejs": "~3.0.2",
"magnific-popup": "~1.0.0",
"jquery-animateNumber": "https://github.com/aishek/jquery-animateNumber.git#v0.0.10",
"MetroJS": "https://github.com/drewgreenwell/MetroJS.git#b70806b32e070073c17677402e64ed909e24a6ef",
"skycons": "https://github.com/darkskyapp/skycons.git#60d41374ea97c5da1f92622ddcecca6dd1d9beed",
"ngmap": "^1.18.0",
"angular-ui-select2": "^0.0.5"
},
"private": true,
"ignore": [
"**/.*",
"vendor"
],
"resolutions": {
"jquery": "~2.1.3",
"d3": "~3.5.4",
"underscore": ">=1.5.0",
"bootstrap": "~3",
"angular": "~1.3.14",
"bootstrap-select": "~1.6.3",
"jquery-ui": "~1.11.2"
}
}
更新
当不使用模态但仅使用div时,问题仍然存在。当我隐藏div并再次显示时,地图全部显示为灰色。有趣的是,当我打开Chrome控制台时,地图会显示出来。
答案 0 :(得分:3)
尝试在初始化地图后显式调用resize
事件:
NgMap.getMap().then(function (map) {
google.maps.event.trigger(map, "resize");
});
实施例
angular.module('app', ['ngMap', 'ui.bootstrap'])
.controller('myCtrl', function ($scope, $uibModal) {
$scope.showModal = function () {
$scope.modal = $uibModal.open({
templateUrl: 'storeShowModal.html',
controller: 'ModalShowStore',
resolve: {
store: function () {
return { "latitude": 51.5285578, "longitude": -0.242023 };
}
}
});
};
$scope.closeModal = function () {
$scope.modal.close();
};
})
.controller('ModalShowStore', function ($scope,$uibModalInstance, NgMap, store) {
$scope.center = [store.latitude,store.longitude];
$scope.position = [store.latitude,store.longitude];
NgMap.getMap().then(function (map) {
google.maps.event.trigger(map, "resize");
});
$scope.store = store;
$scope.cancel = function () {
$uibModalInstance.close();
};
});
&#13;
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script data-require="angularjs-google-maps@1.13.14" data-semver="1.13.14" src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<script data-require="ui-bootstrap@2.5.0" data-semver="2.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maps.google.com/maps/api/js"></script>
<div ng-app="app">
<script type="text/ng-template" id="storeShowModal.html">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" data-ng-click="cancel()" aria-hidden="true">×</button>
<h4 class="modal-title text-align-center fw-bold mt" id="myModalLabel18">{{ store.name }}</h4>
</div>
<div class="modal-body bg-gray-lighter">
<div class="row">
<div class="col-md-12">
<ng-map center="{{ center }}" zoom="10">
<marker position="{{ position }}"></marker>
</ng-map>
</div>
</div>
</div>
</script>
<div ng-controller="myCtrl">
<div>
<button ng-click="showModal()">Show Modal</button>
</div>
</div>
</div>
&#13;
答案 1 :(得分:-1)
我找到了一个非常非常便宜的解决方案,但它起初并不像我想要的那样,但它现在可以工作了。如果更改了高度,地图将能够显示自己。为什么class="hidden"
没有,或style="display:none"
,我不知道......好吧,这样可行:
$scope.openMap = function (id) {
var map = document.getElementById('map' + id);
if (map.style.height == '400px') {
map.style.height = '0px';
} else {
map.style.height = '400px';
}
}