drtwqc3744 2015-02-26 19:59
浏览 21

在Javascript中引用Go数组

I have a Golang array I'm passing to my html file on the front end.

I know that

'{{ index .Array 0}}' 

works and pulls the first element from the array. But I want to do a Javascript for-loop and print every element in the array like so

<script type="text/javascript">
function loop() {

    html = ""
    for(var i = 0; i<5; i++) {
        html += "{{ index .Array " + i + "}}";
      }
}

But this doesn't work. Something about separating the go array index string, HTML/Javascript doesn't like it and it won't load.

It's a syntactical error that I just can't pin down.

Any ideas?

  • 写回答

2条回答 默认 最新

  • dsgk0386 2015-02-26 21:53
    关注

    You need to understand something:

    Template actions such as {{index .Array 0}} are executed at server side in your Go application.

    Javascript code is interpreted and run at client side in the browser.

    The template parameter value used in template actions (Array in your example) does not exist at client side (e.g. as a Javascript object). And Javascript code is not run by the template engine. So the template parameter (value) and Javascript (execution) live in 2 different "spaces".

    Having said that, it is not possible to mix template actions/variables and Javascript execution.

    You have 2 options:

    1) Do what you want to do with template actions.

    2) Use the template to create Javascript code which when executed at the client side will construct/recreate the array as a Javascript object so it will be available for further Javascript processing.

    Note that if you just want to loop over the array once, creating a Javascript array is not neccessary, you can simply render the JavaScript code that would be the loop body inside a {{range}} template action. See Simon's answer as an example to this.

    Elaborating #1

    You can use the {{range .Array}} action to loop over Array, and the block is executed for each element, pipeline set to the array element so you can output the array elements like this:

    {{range .Array}}
        {{.}}
    {{end}}
    

    Of course you can put anything else inside the block, not just the array elements. You can even access the current index like this:

    {{range $idx, $value := .Array}}
        Index = {{$idx}}; Element = {{$value}}<br>
    {{end}}
    

    Elaborating #2

    Let's say your Array contains int numbers, you can recreate it in Javascript and loop over it in Javascript with a template like this:

    <script>
        var arr = [
            {{range .Array}}
                {{.}},
            {{end}}
        ];
    
        // Now you have a javascript array: arr, loop over it to do something:
        html = "";
        for(var i = 0; i < arr.length; i++) {
            html += " " + arr[i];
        }
    </script>
    

    Or since the template engine supports "rendering" arrays and slices as JavaScript arrays, you can simply write:

    <script>
        var arr = {{.Array}};
    
        // Now you have a javascript array: arr, loop over it to do something:
        html = "";
        for(var i = 0; i < arr.length; i++) {
            html += " " + arr[i];
        }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?