How do I pass variable from a Jade template file to a Javascript file?

前端 未结 1 449
迷失自我
迷失自我 2021-01-16 08:18

Im very new to Node.js and trying to get my head round few things.How do I pass variable from a Jade template file to a JS file?

I have got this line of code in my

1条回答
  •  一整个雨季
    2021-01-16 08:46

    Try adding a . character after the script, this tells Jade that you want a block of text inside a tag.

    Here's your updated code.

    html
        head
            script(type='text/javascript').
                var data = !{data}
            link(rel='stylesheet', href='/stylesheets/style.css')
        body
            title Title
            h1 Heading
            #div.test
    

    EDIT - To set title to data.val

    First you should change your response, remove JSON.stringify so you're returning an object not a string value.

    res.render("aws.jade", {data : {'val' : 'This is a Test'}});
    

    Then you should be able to access the data objects attributes and structure directly using dot notation, and assign it straight to the title element like this...

    title= data.val
    

    0 讨论(0)
提交回复
热议问题