我想传递一个使用EJS渲染的Object,它们传递给聚合物元素。
为什么我得到addressLine1":"123" abc"
,空格字符有什么问题,有没有办法将对象传递给聚合物元素,而不像这样在HTML上公开它。
EJS
<g-map customer=<%- JSON.stringify(customer) %> vehicles = <%= JSON.stringify(vehicles) %> ></g-map>
渲染后:
<g-map customer="{"_id":"5658013d7e6908d4b370c3f0","coordinates":[10.764221,106.656368],"phone":"09090909","addressLine1":"123" abc","email":"customer1@gmail.com"}="">
路线档案
res.render('vehicles',
{
title: 'Vehicles Managerment',
customer : req.session.customer,
vehicles : body.data
});
客户对象
"customer": {
"_id": "5658013d7e6908d4b370c3f0",
"coordinates": [
12345,
12345
],
"addressLine1": "123 ABC",
"email": "customer1@gmail.com",
"username": "customer1@gmail.com"
}
答案 0 :(得分:1)
经过研究,我发现我必须将渲染对象放在单引号中,所以它会以正确的方式处理空格。
<g-map customer='<%- JSON.stringify(customer) %>' vehicles = '<%= JSON.stringify(vehicles) %>'