AngularJS指令中的jQuery代码

跳过

我希望以下元素显示Google Map:

<div id="map" class="gmaps" ></div>

为此,我添加了以下js:

<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>

以下脚本代码正在调用该ContactUs.init()方法:

<script type="text/javascript">
    jQuery(document).ready(function() {
        ContactUs.init();
    });
</script>

此代码在contact-us.js文件内部,如下所示:

var ContactUs = function () {
    return {
        //main function to initiate the module
        init: function () {
            var map;
            $(document).ready(function(){
              map = new GMaps({
                div: '#map',
                lat: 41.156863,
                lng: -81.855722
            });
            var marker = map.addMarker({
                lat: 41.156863,
                lng: -81.855722,
                title: 'Foo Inc.',
                infoWindow: {
                    content: "<b>Foo Inc.</b><br>123 Bar Ave, Fred 940<br>San Francisco, CA 94124"
                }
            });

               marker.infoWindow.open(map, marker);
            });
        }
    };
}();

如何使用<div id="map" class="gmaps" ></div>元素中的AngularJS属性指令使上面的代码工作

编辑:

TypeError: Cannot read property 'Pool.<InfoWindowView>' of undefined
    at JT (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:56:919))), <anonymous>:3:861)
    at KT (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:56:919))), <anonymous>:4:31)
    at MT.(anonymous function).j (eval at <anonymous> (eval at <anonymous> (eval at <anonymous> (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:56:919))), <anonymous>:6:951)
    at http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:37:1008
    at $f (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:26:58)
    at Fh.map_changed (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:37:978)
    at Ze (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:19:190)
    at Fh.M.set (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:18:853)
    at Fh.(anonymous function).open (http://maps.gstatic.com/cat_js/maps-api-v3/api/js/18/6/%7Bmain,geometry,places%7D.js:37:807)
    at link (http://localhost:3000/javascripts/app.js:219:31) <div id="map" class="gmaps margin-bottom-40" style="height: 400px; position: relative; overflow: hidden; -webkit-transform: translateZ(0px); background-color: rgb(229, 227, 223);"> 

用于部分显示Google Map的代码:

<div class="main">
    <div class="container">
        <div class="row margin-bottom-40">
            <!-- BEGIN CONTENT -->
            <div class="col-md-12">
                <div class="content-page">
                    <div class="row">
                        <div class="col-md-12">
                            <div id="map" class="gmaps margin-bottom-40" style="height:400px;"></div>
                        </div>
                        <div class="col-md-3 col-sm-3 sidebar2">
                            <h3 class="support">Our Contact Info</h3>
                            <!-- Remaining stuff-->
                        </div>
                    </div>
                </div>
            </div>
            <!-- END CONTENT -->
        </div>
    </div>
</div>
玛丽安·班

您必须创建一个指令:

gmaps.js实现:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

app.directive('gmaps', function() {
  return {
    restrict: 'C', // restrict this directive to css class with name gmaps
    link: function() {
      var map = new GMaps({
        div: '#map',
        lat: 41.156863,
        lng: -81.855722
      });

      var marker = map.addMarker({
        lat: 41.156863,
        lng: -81.855722,
        title: 'Foo Inc.',
        infoWindow: {
          content: "<b>Foo Inc.</b><br>123 Bar Ave, Fred 940<br>San Francisco, CA 94124"
        }
      });

      marker.infoWindow.open(map, marker);
    }
  };
});

的HTML:

  <body ng-controller="MainCtrl">
    <div id="map" class="gmaps" style="width:500px; height: 500px"></div>
    <p>Hello {{name}}!</p>
  </body>

标准Google API:

app.directive('gmaps', function($timeout) {
  return {
    restrict: 'C',
    link: function(scope) {

      var myLatlng = new google.maps.LatLng(41.156863,-81.855722);

      var mapOptions = {
        center: myLatlng,
        zoom: 8
      };
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);

      var infowindow = new google.maps.InfoWindow({
        content: "<b>Foo Inc.</b><br>123 Bar Ave, Fred 940<br>San Francisco, CA 94124"
      });

      var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
      });
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
      });

      infowindow.open(map, marker);

    }
  };
});

http://plnkr.co/edit/z4HAIdGaYjNa2waOcrqE?p=preview

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

代码中没有html的Angularjs指令?

来自分类Dev

改进AngularJS指令代码

来自分类Dev

在Angularjs指令中运行jquery插件

来自分类Dev

angularJs输入指令的源代码中的JavaScript双括号

来自分类Dev

如何在 AngularJS 中获取指令内的代码?

来自分类常见问题

从AngularJS中的指令添加指令

来自分类Dev

AngularJS中的指令内部指令?

来自分类Dev

AngularJS:在指令中包装指令

来自分类Dev

AngularJS jQuery指令

来自分类Dev

jQuery步入angularjs指令

来自分类Dev

使用angularjs指令的动态代码

来自分类Dev

Angularjs +无法调用指令代码

来自分类Dev

jQuery Timepicker不会在AngularJS指令中更新模型

来自分类Dev

在AngularJS指令中包装Formstone Wallpaper jquery插件

来自分类Dev

angularJS指令中的Highchart

来自分类Dev

angularjs中的测试指令

来自分类Dev

AngularJs指令中的问号

来自分类Dev

元素与AngularJS指令中的this

来自分类Dev

AngularJS中的属性指令

来自分类Dev

angularjs中的测试指令

来自分类Dev

angularJS指令中的Highchart

来自分类Dev

如何在AngularJS中运行jQuery代码?

来自分类Dev

如何在 Angularjs 中编写 jquery 代码?

来自分类Dev

jQuery Cycle插件的Angularjs指令

来自分类Dev

来自jQuery插件的angularjs指令

来自分类Dev

ElevateZoom jQuery插件的AngularJS指令

来自分类Dev

如何使用transclude将代码添加到指令中并在AngularJS中传递参数?

来自分类Dev

入门angularjs-在指令中包含指令

来自分类Dev

AngularJS中的指令到指令通信