[英]How can I pass JSON to client using Node.js + HBS + Express?
Consider, I am sending JSON to client as follows in render page of Express (using hbs as a view engine): 考虑一下,我在Express的渲染页面中将JSON发送到客户端,如下所示(使用hbs作为视图引擎):
res.render('MyPage.html', { layout: false, PageTitle: 'Project Name', JSON_Data: {field1:'value',field2:'value2'}});
I am able to access and set title of html page by using {{PageTitle}}
, following is the code. 我可以使用
{{PageTitle}}
来访问和设置html页面的标题,下面是代码。
<title>{{PageTitle}}</title>
Now I want to show JSON_data
into alert popup. 现在,我想将
JSON_data
显示到警报弹出窗口中。
I've tried following way, but getting Uncaught SyntaxError: Unexpected token {
, while debugging in chrome is shows var jsonobj = [object Object]
我已经尝试过以下方法,但是遇到
Uncaught SyntaxError: Unexpected token {
,而在chrome中调试var jsonobj = [object Object]
显示var jsonobj = [object Object]
function fbOnBodyLoad() {
var jsonobj = {{JSON_data}};
alert(jsonobj);
}
Could any body give some idea on How to access JSON_data
and show in alert 任何人都可以对如何访问
JSON_data
并在警报中显示出一些想法JSON_data
Advance Thanks 提前谢谢
to access the inner elements of the json object, try like this 访问json对象的内部元素,尝试像这样
var jsonobj = "{{JSON_data.field1}}";
may be this might solve the issue. 可能这可以解决问题。
refer 参考
Handlebars.js parse object instead of [Object object] Handlebars.js解析对象,而不是[Object object]
You could always register a Handlebars helper to format the object into a string format (such as by using JSON.stringify), and then use that helper in your view. 您总是可以注册一个Handlebars助手来将对象格式化为字符串格式(例如使用JSON.stringify),然后在视图中使用该助手。 http://handlebarsjs.com/block_helpers.html
http://handlebarsjs.com/block_helpers.html
For me it worked by doing in the server: 对我来说,它是通过在服务器中完成工作的:
res.render('pages/register', {
title: 'Register Form',
messages: req.flash('error'),
countries:JSON.stringify(countries)
});
and then I used this countries variables to assign in angular controller like this: 然后我使用这个国家/地区变量在角度控制器中进行如下分配:
angular.module('languagesApp', []).controller('DemoController', function($scope) {
$scope.algo = {{-countries}};
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.