小编Ana*_*man的帖子

Pass object as parameter to onclick method in handlebars template

I have a JSON that looks like this:

{
list: [
        { name: 'AAA',
          id: 1,
          age: 34
        },
        { name: 'BBB',
          id: 2,
          age: 24
        }
      ]
}
Run Code Online (Sandbox Code Playgroud)

And a template like this:

<ul>
    {{#each list}}
        <li onclick="someFunc({{this}})">{{name}} ({{age}}) </li>
    {{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud)

Basically I just want to pass the current object , to a function that does something with it.

Now if I try it, then the generated HTML just has

 ... onclick="someFunc( [object Object] )" ...
Run Code Online (Sandbox Code Playgroud)

whereas I'd …

javascript json handlebars.js

5
推荐指数
1
解决办法
7805
查看次数

标签 统计

handlebars.js ×1

javascript ×1

json ×1