I'm passing data to render template:
res.render 'index', {data: ['a', 'b']}, function(err, html) {
});
in the template, I'd like to render the array ['a', 'b']
as an array in javascript:
script(type='text/javascript').
var arr = #{data};
but they are rendered as [a, b]
, an array of variables, what I want is variable names: ['a', 'b']
.
Thanks
First you need to JSON.stringify
the data property object.
...
data: JSON.stringify(['a', 'b'])
...
Then in your Jade Template use !{}
.
var arr = !{data}; // ["a","b"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.