简体   繁体   English

angular.js路由和地图问题

[英]Problems with angular.js routing and maps

I have a problem that when i using a Routing, I don't understand why but maps duplicate, instead one map i have two. 我有一个问题,当我使用路由时,我不明白为什么,但是地图重复,而是一张地图我有两张。

Html file: HTML文件:

<div id="contacts" ng-controller="twoCtrl">
    <section class="map-wrapper">
        <div class="maps" id="mapOne"></div>
        <div class="maps" id="mapTwo"></div>
    </section>
</div>

Css : section.map-wrapper { margin-top: 30px; CSS:section.map-wrapper {margin-top:30px; position: relative; 职位:相对 height: 310px; 高度:310像素; width: 100%; 宽度:100%; } }

section.map-wrapper .maps {
    position: absolute;
    height: auto;
    height: 310px;
    width: 100%;
    z-index: 0;
    opacity: 0;
    transition: opacity .5s;
}

section.map-wrapper .maps.active {
    z-index: 1;
    opacity: 1;
}

js js

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "main.html",
        controller: 'oneCtrl'
    })
    .when("/contacts", {
        templateUrl : "pages/contacts.html",
        controller: 'twoCtrl'
    })
});
app.controller('twoCtrl',function($scope){
    $(function() {
        //maps' active classes
            maps();
            function maps() {
                $(".load-block:first").addClass('active');
                $(".maps:first").addClass('active');
                $(".office-block[id='office']").addClass("active");
                $("#office").click(function() {
                    $(".office-block[id='branch']").removeClass("active");
                    $(".office-block[id='office']").addClass("active");
                    $(".maps").removeClass("active");
                    $("#mapOne").addClass("active");
                });

                $("#branch").click(function() {
                    $(".office-block[id='office']").removeClass("active");
                    $(".office-block[id='branch']").addClass("active");
                    $(".maps").removeClass("active");
                    $("#mapTwo").addClass("active");
                });
            };

         //map
    ymaps.ready(init);
        var myMap1,
            myMap2,
            myPlacemark,
            myPlacemark2

        function init(){ 
            myMap1 = new ymaps.Map("mapOne", {
                center: [50.0094033, 36.2345667],
                zoom: 16,
                controls: []
            }); 

            myMap2 = new ymaps.Map("mapTwo", {
                center: [49.7118034, 37.6141396],
                zoom: 16,
                controls: []
            }); 

            myPlacemark = new ymaps.Placemark([50.0094033, 36.2345667], {
                hintContent: 'Охрана и технологии',
                balloonContent: 'г. Харьков, ул. Данилевского, 12 офис 22'
            }, {
                iconLayout: 'default#image',
                iconImageHref: 'includes/icons/map-mark.png',
                iconImageSize: [35, 35]
            });

            myPlacemark2 = new ymaps.Placemark([49.7118034, 37.6141396], {
                hintContent: 'Охрана и технологии',
                balloonContent: 'г.Купянск, пл.Ленина, оф.34'
            }, {
                iconLayout: 'default#image',
                iconImageHref: 'includes/icons/map-mark.png',
                iconImageSize: [35, 35]
            });

            myMap1.geoObjects.add(myPlacemark);
            myMap2.geoObjects.add(myPlacemark2);
        }
    });

});

Just remove ng-controller="twoCtrl" from your html code 只需从您的html代码中删除ng-controller =“ twoCtrl”

<div id="contacts" ng-controller="twoCtrl">

You have already assigned it in your $routeConfig 您已经在$ routeConfig中分配了它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM