gpt4 book ai didi

javascript - 是否可以将 ejs 变量传递给嵌入式 JavaScript 文件

转载 作者:行者123 更新时间:2023-11-30 21:06:08 25 4
gpt4 key购买 nike

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

app.js 使用国家代码数组(即 ["MEX","USA"])呈现 index.ejs 文件。然后 index.ejs 调用 countrymap.js,它使用 d3 在国家中使用 json 呈现 map .geo.json.

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

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

谢谢,乔治

索引.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>

应用程序.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.")
})

国家 map .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);});

});

最佳答案

在您的 index.ejs 中声明一个变量文件就在这个 <script src="js/countrymap.js" type="text/javascript"></script> 之上例如:

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

然后使用javascript变量get_countries在你的countrymap.js文件。

关于javascript - 是否可以将 ejs 变量传递给嵌入式 JavaScript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46571298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com