[英]Passing variable from jade file to javascript file
I am making a node express app. 我正在制作一个节点快速应用程序。 I am trying to pass a value from jade to a javascript file.
我试图将值从玉传递给javascript文件。
On the server side I am using: 在服务器端,我正在使用:
res.render('index', { title: 'Title', test: 1 });
In my index.jade file I have the following: 在我的index.jade文件中,我具有以下内容:
script(type='text/javascript', src='http://code.jquery.com/jquery-1.9.1.js')
script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.js')
script(type='text/javascript', src='./javascripts/ui.js')
script(type='text/javascript', src='./javascripts/handle.js').
var data = !{test}
block content
h3= title
In my handle.js file I have 在我的handle.js文件中
$(function() {
console.log(data);
});
On the console I get a message saying data is not defined. 在控制台上,我收到一条消息,提示未定义数据。 How do I pass the value of test from the jade file to the javascript file?
如何将test的值从jade文件传递到javascript文件? Currently
test
is just an interger, but eventually it will be an object which contains a lot of properties. 当前
test
只是一个整数,但最终它将是一个包含很多属性的对象。
How do I properly pass the value from the jade file to the javascript file? 如何正确将值从玉文件传递到javascript文件?
That will only work for literals. 那仅适用于文字。 For objects you can do this:
对于对象,您可以执行以下操作:
var data = !{JSON.stringify(data)};
As for the showing undefined part, check if its getting properly set and it is the correct variable. 至于显示的未定义部分,请检查它的设置是否正确以及它是正确的变量。
Make sure you are loading in correct order: 确保以正确的顺序加载:
script(type='text/javascript')
var data =!{JSON.stringify(test)};
script(src='./javascripts/handle.js)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.