【问题标题】:Is it possible to pass an ejs variable to embedded JavaScript File是否可以将 ejs 变量传递给嵌入式 JavaScript 文件
【发布时间】:2018-03-16 05:27:08
【问题描述】:

我有四个文件,我正在为这个项目使用节点。 我有 index.ejsapp.jscountymap.jscountries.geo.json。。 p>

app.js 使用国家代码数组(即 ["MEX","USA"] 渲染 index.ejs 文件。然后 index.ejs 调用 countrymap.js 使用 d3 使用国家/地区的 json 渲染地图.geo.json。

我想做的是将传递给 index.ejs 的国家代码,并使用它们根据 countries.geo.json 文件为具有相应国家代码的国家着色。到目前为止,我已经尝试了几种方法,但似乎都没有奏效。

如果有人有任何想法,我真的可以使用帮助。这是我最后的投资组合和平,我被困在第一个障碍上。另外,如果您认为我做错了一切并有另一种解决方案,我也很想听听。

谢谢, 乔治

index.ejs

 <!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Passport Social</title>

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
    <script type="text/javascript" src="https://d3js.org/topojson.v2.min.js"></script>

</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <div class="list-group">
                    <li class="list-group-item active">Info 1</li>
                    <li class="list-group-item">Info 2</li>
                    <li class="list-group-item">Info 3</li>
                </div>
            </div>
            <div class="col-md-9">
                <div class="thumbnail">
                    <svg style="background:grey;"></svg>
                </div>
            </div>
        </div>
    </div>

    <script src="js/countrymap.js" type="text/javascript"></script>



</body>


</html>

app.js

var express     = require("express"),
    app         = express(),
    bodyParser  = require("body-parser");


var countrydata = ["MEX","USA"];

app.get("/", function(req, res){
    res.render("index",{countries:countrydata});
});

app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
app.use(express.static(__dirname+"/public"));


app.listen(process.env.PORT,process.env.IP,function(){
    console.log("Passport Social Server is Running.")
})

countrymap.js

/*global d3*/

var w = 500,
    h = 300;

var projection = d3.geo.mercator()
        .scale(1)
        .translate([0, 0]);
var path = d3.geo.path()
        .projection(projection);

var svg = d3.select("svg")
    .attr("width", w)
    .attr("height", h);

d3.json("json/countries.geo.json", function(error, world) {
  if (error) return console.log(error);
  world.features.forEach(function(path){
  })

  var b = path.bounds(world),
      s = .95 / Math.max((b[1][0] - b[0][0]) / w, (b[1][1] - b[0][1]) / h),
      t = [(w - s * (b[1][0] + b[0][0])) / 2, (h - s * (b[1][1] + b[0][1])) / 2];
  projection.scale(s)
      .translate(t);

  svg.selectAll("path")
      .data(world.features).enter()
      .append("path")
      //.style("fill", "black")
      .style("stroke", "grey")
      .style("stroke-width", "1px")
      .attr("d", path)
      .attr("id", function(d, i) {return (d.id);});

});

【问题讨论】:

  • 你试过&lt;% variable_name %&gt; ??
  • 这在 js 文件中是不允许的。

标签: javascript node.js ejs


【解决方案1】:

在您的 index.ejs 文件中声明一个变量,就在这个 &lt;script src="js/countrymap.js" type="text/javascript"&gt;&lt;/script&gt; 上方 例如:

<script type="text/javascript">
    var get_countries = <% countries %>
</script>
<script src="js/countrymap.js" type="text/javascript"></script>

然后在您的countrymap.js 文件中使用javascript 变量get_countries

【讨论】:

  • 也许是&lt;%- JSON.stringify(countries) %&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
相关资源
最近更新 更多