javascript - app.directive Google map 在托管后无法工作

标签 javascript c# asp.net-mvc angularjs google-maps

我正在使用 angularjs 处理 MVC 5 Web 应用程序。有一个谷歌地图。

在我的 Angular View 中,我使用 app.js 中的指令来显示 google map 。

请参阅我的下面的代码。

app.js

myApp.directive('googlemap', function ($compile) {
return {
    controller: function ($scope) {
        var map;

        this.registerMap = function (myMap) {
            var center = myMap.getCenter(),
              latitude = center.lat(),
              longitude = center.lng();
            //zoom = center.zoom();

            map = myMap;
            $scope.latitude = latitude;
            $scope.longitude = longitude;
            $scope.zoom = map.getZoom();
        };

        $scope.$watch('latitude + longitude', function (newValue, oldValue) {
            if (newValue !== oldValue) {
                var center = map.getCenter(),
                  latitude = center.lat(),
                  longitude = center.lng();
                if ($scope.latitude !== latitude || $scope.longitude !== longitude)
                    map.setCenter(new google.maps.LatLng($scope.latitude, $scope.longitude));
            }
        });
    },
    link: function (scope, elem, attrs, ctrl) {
        var mapOptions,
          latitude = attrs.latitude,
          longitude = attrs.longitude,
          //controlTemplate,
          //controlElem,
          map;

        // parsing latLong or setting default location
        latitude = latitude && parseFloat(latitude, 10) || 43.074688;
        longitude = longitude && parseFloat(longitude, 10) || -89.384294;

        mapOptions = {
            zoom: 8,
            disableDefaultUI: true,
            center: new google.maps.LatLng(latitude, longitude),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(elem[0], mapOptions);

        ctrl.registerMap(map);

        function centerChangedCallback(scope, map) {
            return function () {
                var center = map.getCenter();
                scope.latitude = center.lat();
                scope.longitude = center.lng();
                scope.zoom = map.getZoom();
                if (!scope.$$phase) scope.$apply();
            };
        }
        google.maps.event.addListener(map, 'center_changed', centerChangedCallback(scope, map));
    }
};
});

HTML

<div style="height:400px;" googlemap latitude="43.074688" longitude="-89.384294"></div>

它在本地主机上运行良好。但是当我在网络服务器上托管此页面时,

Google Chrome 控制台中显示错误:

enter image description here

我发现两个页面(本地主机和托管)的 view-source: 存在差异 页)。本地主机和托管页面中的脚本引用很少。

您对这个问题有什么建议吗?

最佳答案

您好,我很确定问题出在加载您的脚本上,请交叉检查脚本在主机中的位置。

关于javascript - app.directive Google map 在托管后无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30543302/

相关文章:

javascript - Controller 在 Ember 中更改模型的事件?

javascript - 如何在溢出隐藏父元素内显示绝对元素

c# - 通过 XML 属性扩展 ASP.Net TreeView 中的节点

jquery - Access-Control-Allow-Origin header 存在于请求的资源上

javascript - 在 ASP.NET MVC 中使用 Jquery 根据选定的下拉列表更新表?

c# - Asp.Net 5 TagHelper 模型状态绑定(bind)

javascript - 如何根据 JavaScript 中哪个 nodeList 不为空来设置变量?

javascript - 如何在 d3 工具提示中添加点击事件?

c# - .NET 中 ArgumentException.Message 的本地化

c# - 在 Visual Studio 2010 中将 Excel 数据导入 DataGridView