问题描述
我有一个JSON,如下所示:
{
list:[
{name :'AAA',
id:1,
年龄:34
},
{名称:'BBB',
id:2,
年龄:24
}
]
}
这:
< ul>
{{#each list}}
onclick =someFunc({{this}})> {{name}}({{age}})< / li>
{{/ each}}
< / ul>
基本上我只是想将当前对象传递给一个可以处理它的函数。 p>
现在,如果我尝试一下,那么生成的HTML只有
。 .. onclick =someFunc([object Object])...
然而我想它是这样的:
... onclick =someFunc({name:'AAA',id:1,age :34})...
我该如何解决这个问题?
发布供将来参考:
从这里得到我的答案:
原来,在粘贴到模板之前,车把对数据做了一个toString。我只需要注册一个帮助器方法,将其转换回json。
函数(上下文){return JSON.stringify(context);
});
< li onclick =someFunc({{json this}})> {{name}}({{age}})< / li>
I have a JSON that looks like this:
{
list: [
{ name: 'AAA',
id: 1,
age: 34
},
{ name: 'BBB',
id: 2,
age: 24
}
]
}
And a template like this:
<ul>
{{#each list}}
<li onclick="someFunc({{this}})">{{name}} ({{age}}) </li>
{{/each}}
</ul>
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] )" ...
whereas I'd like it to be like this:
... onclick="someFunc( {name: 'AAA', id: 1, age: 34} )" ...
How can I fix this?
Posting for future references:
Got my answer from here:
Handlebars.js parse object instead of [Object object]
Turns out Handlebar does a toString on the data before pasting into the template. All I had to do was to register a helper method that converts it back to json.
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context);
});
<li onclick="someFunc({{json this}})">{{name}} ({{age}}) </li>
这篇关于将对象作为参数传递给句柄模板中的onclick方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!