简体   繁体   English

将变量从jade传递给javascript

[英]passing a variable from jade to javascript

I am trying to pass a variable from the route handler to the javascript file. 我试图将路由处理程序中的变量传递给javascript文件。

express route that fetches home page is: 获取主页的快递路线是:

exports.home = function(req, res){

    var _ajaxData = [ ["#collections","collections", "Collections"],
                      ["#candidates","candidates", "Candidates"],
                      ["#entries","entriess", "Entries"],
                      ["#exits","exits", "Exits"]
                    ];

    res.render('home/home', { title: 'Welcome to My Web', _ajaxData: _ajaxData});
};

And my jade file looks like the following: 我的玉文件如下所示:

extends ../layout/base

block content
      each item in _ajaxData
        div(id= item[0]).col-md-6
              h3.panel-title= title
            .panel-body
              | Loading content...

  script(src='js/home.js')

And the content is loaded using ajax in home.js that looks like the following: 并使用home.js中的ajax加载内容,如下所示:

var ajaxData = JSON.stringify('!{_ajaxData}');
console.log(ajaxData);
// Further processing here

Problem: The console prints: 问题:控制台打印:

"!{_ajaxData}" 

where I am looking to get the full array passed to the javascript. 我希望将完整的数组传递给javascript。

Appreciate any insights 欣赏任何见解

Add this to your jade file 将其添加到您的玉器文件中

script(type='text/javascript')                                                   
  var local_data =!{JSON.stringify(data)};   
script(src='javascripts/myScript.js')  

All your data should now be in local_data for your use in myScript.js 现在,您的所有数据都应该在local_data ,以便在myScript.js使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM