当我有Google Maps API代码段时:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
Run Code Online (Sandbox Code Playgroud)
在index.html,我得到了错误:Uncaught InvalidValueError: initMap is not a function.
我希望在我的Yeoman-scaffolded AngularJS Web应用程序中保留我的所有bower_componentCSS,API和脚本声明index.html.我实际上试图重新创建的地图将在另一条路线上,我们将其称为"afterlogin"路线,只是一张基本地图.我将js和html html组件分成了afterlogin.js和afterlogin.html
造成这种情况的原因有很多.其中一个是在调整调用以匹配命名空间的问题,/sf/answers/2412677711/.这需要角度服务吗?如果是这样,该服务如何initMap在Google map api片段中使用该功能及其调用?
其中一个复杂因素是订单.我是新来的Web应用开发,但我可以告诉的index.html第一负载,使用在片断的URL进行回调到initMap未在特色功能<script>...</script>的index.html文件,也没有在app.js文件中.相反,由于init函数位于路径的js代码中,因此无法看到它,因此需要某种类型的"命名空间"版本的调用.这甚至从登录路线导致控制台错误,这甚至不是div地图的位置.
----编辑:----
还要注意,在这种情况下,这没有做到这一点:
window.initMap = function(){
//...
}
Run Code Online (Sandbox Code Playgroud)
这也不适用,因为函数永远不会被调用:Uncaught InvalidValueError:initMap不是函数
- - - - - -
afterlogin.js
angular.module('myappproject').controller('AfterloginCtrl', function ($scope) {
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), …Run Code Online (Sandbox Code Playgroud)