发送数组在手柄中的功能? [英] Send array to function in handlebars?

查看:87
本文介绍了发送数组在手柄中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



I have the following piece of code...

 var mainArray = 
{
    nestedArrays:
    {
        array1:[... items ...],
        array2:[... items ...]
    }
};

var source =
    '{{#nestedArrays}}' +
        '{{#each this}}' +
            '<div class="listItem" onclick="populateSecondMenu({{SEND THIS ARRAY!}});">' +
                '<div class="leftSide">' +
                    '<div class="listTitle">Indicator : {{this.length}} </div>' + 
                '</div>' +
            '</div>' +
        '{{/each}}' +          
        '{{/items}}';

var template = Handlebars.compile(source);

$('.list').html(template(mainArray));

正如您已经看到的那样,我可以遍历这个结构,并且将长度这两个阵列1和阵列2在用户界面上的列表项目内。

As you can already see here, I am able to iterate over this structure, and place the length of both "array1" and "array2" inside list items on the UI.

然而,我也希望能够做的 - 是能够通过当前item在#each标签内时可以使用 - 请参阅函数调用populateSecondMenu?我想把我现在的阵列 - 这样我可以通过它,我该怎么做?

However, What I also want to be able to do - is to be able to pass the current item to a the function when inside the "#each" tags - see that function call, "populateSecondMenu"? I want to put the array I am at now - so that I can pass that in, How might I do that?

预先感谢!

推荐答案

试试这个。我已经使用参数

try this. I have used arguments

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

console.log(arguments);
}
</script>

<div class="entry">
  <h1></h1>
  <div class="body">
     {{#nestedArrays}}
        {{#each this}}
            <div class="listItem" onclick="populateSecondMenu({{this}})">
                <div class="leftSide">
                    <div class="listTitle">Indicator : {{this.length}} </div> 
               </div>
            </div>
        {{/each}}         
        {{/nestedArrays}}
  </div>
</div>

这篇关于发送数组在手柄中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆