简体   繁体   English

从 Meteor 模板注入 Javascript

[英]Injecting Javascript From Meteor Template

I'm pulling long, lat coords from my collection and I want to generate dynamic Google Map Markers onto the map in the page.我正在从我的收藏中拉长经纬度坐标,我想在页面中的地图上生成动态 Google 地图标记。

The data is populating fine in the source, but the markers fail to show on the page.数据在源中填充良好,但标记无法显示在页面上。

Is it possible to inject a javascript object into the <script></script> tags?是否可以将 javascript 对象注入<script></script>标签?

Javascript Javascript

  Template.maps.onRendered(function() {
    var map = new google.maps.Map(this.find('.map'), {zoom: 8, center: new google.maps.LatLng(33.658206, -111.962827)});
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  });

HTML HTML

<template name="maps">
    <div id="map-canvas"></div>
 </template>

Don't mix html and js.不要混合使用 html 和 js。 Put all the javascript inside methods of relevant template.将所有 javascript 放在相关模板的方法中。

Template.mapWrapper.onRendered(function() {
  var map = new google.maps.Map(this.find('.map'), {...});
  _.each(locations, function(location) {
    marker = new google.maps.Marker({...});
  });
});

You should also use classes or data parameters instead of element id, but most importantly, don't mix html and js.您还应该使用类或数据参数而不是元素 id,但最重要的是,不要混合使用 html 和 js。

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

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