全局分配的变量未显示

时间:2016-04-30 05:53:22

标签: node.js pug

我正在加载一个数据数组,将每个数组分配给一个全局变量,然后尝试显示它。出于某种原因,我没有为#{alibaba}显示一个值。

script(type='text/javascript', src="http://code.jquery.com/jquery-1.9.1.min.js")
    body
      script(type='text/javascript').
        var microsoft;
        var amazon;
        var apple;
        var mcdonalds;
        var berkshire;
        var alibaba;
        $.getJSON('../json', function(data) {
            microsoft = data[0];
            amazon = data[1];
            apple = data[2];
            mcdonalds = data[3];
            berkshire = data[4];
            alibaba = data[5];
        });
      p Alibaba Stock Price Is: #{alibaba}

这是我从../ json

加载的数组
[
    "49.87",
    "93.74",
    "145.48",
    "659.59",
    "126.49",
    "76.94"
]

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

script(type='text/javascript', src="http://code.jquery.com/jquery-1.9.1.min.js")
    body
      script(type='text/javascript').
        var microsoft;
        var amazon;
        var apple;
        var mcdonalds;
        var berkshire;
        var alibaba;
        $.getJSON('../json', function(data) {
            microsoft = data[0];
            amazon = data[1];
            apple = data[2];
            mcdonalds = data[3];
            berkshire = data[4];
            alibaba = data[5];

            // when json is loaded we place data into tag span
            $("#alibaba").text(alibaba);
        });
      p Alibaba Stock Price Is: 
        span#alibaba