Consider, I am sending JSON to client as follows in render page of Express (using hbs as a view engine):
考虑一下,我在Express的呈现页面(使用hbs作为视图引擎)中向客户发送JSON如下:
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显示为alert popup。
I've tried following way, but getting Uncaught SyntaxError: Unexpected token {
, while debugging in chrome is shows var jsonobj = [object Object]
我尝试了以下方法,但未被捕获的SyntaxError:意外令牌{,而在chrome中调试显示var jsonobj = [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并在警报中显示
Advance Thanks
提前谢谢你
3 个解决方案
#1
0
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. http://handlebarsjs.com/block_helpers.html
您可以注册一个Handlebars helper以将对象格式化为字符串格式(例如使用JSON.stringify),然后在视图中使用这个helper。http://handlebarsjs.com/block_helpers.html
#2
1
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]
车把。js解析对象而不是[对象对象]
#3
0
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}};
...
#1
0
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. http://handlebarsjs.com/block_helpers.html
您可以注册一个Handlebars helper以将对象格式化为字符串格式(例如使用JSON.stringify),然后在视图中使用这个helper。http://handlebarsjs.com/block_helpers.html
#2
1
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]
车把。js解析对象而不是[对象对象]
#3
0
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}};
...