将变量从 jade 传递给 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21263337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing a variable from jade to javascript
提问by Kiran
I am trying to pass a variable from the route handler to the javascript file.
我正在尝试将变量从路由处理程序传递到 javascript 文件。
express route that fetches home page is:
获取主页的快递路线是:
exports.home = function(req, res){
var _ajaxData = [ ["#collections","collections", "Collections"],
["#candidates","candidates", "Candidates"],
["#entries","entriess", "Entries"],
["#exits","exits", "Exits"]
];
res.render('home/home', { title: 'Welcome to My Web', _ajaxData: _ajaxData});
};
And my jade file looks like the following:
我的玉文件如下所示:
extends ../layout/base
block content
each item in _ajaxData
div(id= item[0]).col-md-6
h3.panel-title= title
.panel-body
| Loading content...
script(src='js/home.js')
And the content is loaded using ajax in home.js that looks like the following:
内容是在 home.js 中使用 ajax 加载的,如下所示:
var ajaxData = JSON.stringify('!{_ajaxData}');
console.log(ajaxData);
// Further processing here
Problem: The console prints:
问题:控制台打印:
"!{_ajaxData}"
where I am looking to get the full array passed to the javascript.
我希望将完整数组传递给 javascript。
Appreciate any insights
欣赏任何见解
回答by bzupnick
Add this to your jade file
将此添加到您的玉文件中
script(type='text/javascript')
var local_data =!{JSON.stringify(data)};
script(src='javascripts/myScript.js')
All your data should now be in local_data
for your use in myScript.js
您的所有数据现在都应该local_data
可供您使用myScript.js