我可以在每个对象属性上执行jquery-tmpl吗? [英] Can I do a jquery-tmpl each over object properties

查看:84
本文介绍了我可以在每个对象属性上执行jquery-tmpl吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板{{each}}指令非常适合迭代这样的数组:

The template {{each}} directive works great for iterating over an array like this:

var myArray = ["a","b","c"];

我想知道是否存在迭代对象属性的等价物,即:

I'm wondering if there is an equivalent for iterating over object properties, i.e.:

var myObj = {"propOne": "a", "propTwo": "b", "propThree": "c"};

我想要一个模板让我输出为

I'd like a template that would let me output as

<ul>
  <li><span>propOne</span><span>a</span></li>
  .... etc

对于奖励积分,我想使用此模板KnockoutJS。

For bonus points I'd like to use this template from KnockoutJS.

推荐答案

实际上{{each}}将遍历对象的属性。你可以这样做:

Actually {{each}} will walk through properties on an object. You can do something like this:

{{each(prop, val) myObj}}
      <li><span>${prop}</span> - <span>${val}</span></li>
{{/each}}

以下是Knockout中的示例: http://jsfiddle.net/rniemeyer/rpMsM/

Here is a sample in Knockout: http://jsfiddle.net/rniemeyer/rpMsM/

如果你真的想使用模板绑定的 foreach 选项,那么唯一真正的选择是将对象映射到具有键/值属性的对象数组。这样的事情: http://jsfiddle.net/rniemeyer/rpMsM/1/

If you really want to use the foreach option of the template binding, then the only real option is to map the object to an array of objects with key/value properties. Something like this: http://jsfiddle.net/rniemeyer/rpMsM/1/

这篇关于我可以在每个对象属性上执行jquery-tmpl吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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