javascript - ng-include 不适用于其中包含 javascript 的 html

标签 javascript html angularjs google-maps

这是我的 HTML 代码以及 javascript

<!DOCTYPE html>
<html>
<head lang="en">
    <style>
        #map-canvas {
            height:400px;
            width:600px;
        }
        .controls {
            margin-top: 16px;
            border: 1px solid transparent;
            border-radius: 2px 0 0 2px;
            box-sizing: border-box;
            -moz-box-sizing: border-box;
            height: 32px;
            outline: none;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        }
        #pac-input {
            background-color: #fff;
            padding: 0 11px 0 13px;
            width: 400px;
            font-family: Roboto;
            font-size: 15px;
            font-weight: 300;
            text-overflow: ellipsis;
        }
        #pac-input:focus {
            border-color: #4d90fe;
            margin-left: -1px;
            padding-left: 14px;
            /* Regular padding-left + 1. */
            width: 401px;
        }
        .pac-container {
            font-family: Roboto;
        }
        #type-selector {
            color: #fff;
            background-color: #4d90fe;
            padding: 5px 11px 0px 11px;
        }
        #type-selector label {
            font-family: Roboto;
            font-size: 13px;
            font-weight: 300;
        }
    </style>

    <script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <script type="text/javascript">

        var map;
        function initialize() {

            var markers = [];
            var map = new google.maps.Map(document.getElementById('map-canvas'), {
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var defaultBounds = new google.maps.LatLngBounds(
                    new google.maps.LatLng(-33.8902, 151.1759),
                    new google.maps.LatLng(-33.8474, 151.2631));
            map.fitBounds(defaultBounds);

            // Create the search box and link it to the UI element.
            var input = /** @type {HTMLInputElement} */
                    (
                            document.getElementById('pac-input'));
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

            var searchBox = new google.maps.places.SearchBox(
                    /** @type {HTMLInputElement} */ (input));

            // Listen for the event fired when the user selects an item from the
            // pick list. Retrieve the matching places for that item.
            google.maps.event.addListener(searchBox, 'places_changed', function () {

                var places = searchBox.getPlaces();

                for (var i = 0, marker; marker = markers[i]; i++) {
                    marker.setMap(null);
                }

                markers = [];
                var bounds = new google.maps.LatLngBounds();

                for (var i = 0, place; place = places[i]; i++) {

                    // Create a marker for each place.
                    var marker = new google.maps.Marker({
                        map: map,
                        title: place.name,
                        position: place.geometry.location
                    });

                    markers.push(marker);

                    bounds.extend(place.geometry.location);
                }

                map.fitBounds(bounds);
            });

            google.maps.event.addListener(map, 'click', function(event) {
                var myLatLng = event.latLng;
                var lat = myLatLng.lat();
                var lng = myLatLng.lng();
                var x = 'Latitude is =' + lat + 'Longitude is=' + lng;
                var TheTextBox = document.getElementById("lat");
                TheTextBox.value = lat;
                var TheTextBox1 = document.getElementById("long");
                TheTextBox1.value = lng;
                // alert(x);
            });
        }
        google.maps.event.addDomListener(window, "load", initialize);
    </script>
</head>
<body>
<div id="map-canvas"></div>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<input type="text" name="lat" id="lat">
<input type="text" name="long" id="long">

</body>
</html>

我有一个 Angular 应用程序,它在点击事件上调用各种 View ,并且由 $locationprovider$routeprovider 从一个 js 文件处理 Controller 和所有内容。

现在的问题是,当我尝试在其中一个 View 中使用 ng-include 功能时,它无法正常工作。文本框可见,但 map 不可见。我什至尝试使用 onload 来再次初始化该函数,该函数位于我的原始 HTML 页面中,但仍然不起作用。

控制台上的错误是 - ReferenceError: google is not Defined

当 javascript 部分无法使用谷歌地图脚本时会发生这种情况。

检查此错误信息:Reference Error - StackOverflow Question

ng-包含代码

        <ng-include
        src="'testmap.html'"
        [onload="initialize();"]
        >
        </ng-include>

请让我知道我必须采取什么措施来纠正这个问题。

感谢您的宝贵时间。如果您需要我方面的更多说明,请告诉我。

最佳答案

ng-include 不是 iframe 的替代品。它的目的是在其他 Angular 模板中添加模板,其中可能包括对实际 Angular 代码的调用。 您试图将整个 html 页面、head 标签和所有内容包含到另一个页面中,这不仅是无效的 html,而且除非您在 iframe 元素内执行此操作,否则毫无意义。

我建议您将代码重构为谷歌地图 Angular Directive(指令)。

关于javascript - ng-include 不适用于其中包含 javascript 的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35360698/

相关文章:

html - 我的 Tumblr 网站在移动设备上的透明 PNG 后面显示白色,但在桌面设备上不显示,我该如何解决此问题?

javascript - 如何在 angularjs 中使用 json 从 View 更改 Controller

angularjs - 如何使用cordova插件获取ionic中的设备ID?

javascript - Angular 不从复杂对象中选择选择框选项

javascript - 如何在一个地方写一个导航栏,并用 html/css/js 在所有页面上使用它

javascript - 可滚动的 primeng 数据表模板标题

javascript - 如何检测 DOM 可拖动对象和 FabricJS 形状之间的碰撞

javascript - 如何在 ng-repeat 循环中分配一个 angularjs 事件监听器?

html - 如何在居中图像周围制作彩色边框

html - IIS6 : Disallow a particular file extension from being downloaded