Send array to function in handlebars?

微笑、不失礼 提交于 2019-12-12 02:28:56

问题


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));

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.

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?

Thanks in advance!


回答1:


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>


来源:https://stackoverflow.com/questions/34389756/send-array-to-function-in-handlebars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!