3

Consider, I am sending JSON to client as follows in render page of Express (using hbs as a view engine):

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.

  <title>{{PageTitle}}</title>

Now I want to show JSON_data into alert popup.

I've tried following way, but getting Uncaught SyntaxError: Unexpected token {, while debugging in chrome is shows 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

Advance Thanks

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164

3 Answers3

2

to access the inner elements of the json object, try like this

var jsonobj = "{{JSON_data.field1}}";

may be this might solve the issue.

refer

Handlebars.js parse object instead of [Object object]

Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
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

Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31
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}};
...
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164