遍历 html 模板中的数组字段
Iterate over array-field in the html-template
我有一个书签集合:
> db.Bookmarks.find()
{ "Name" : "Lenta.ru", "Timestamp" : 1381233418275, "URL" : "http://lenta.ru", "_id" : "zS2ccZXkGLENzN2Bx", "tags" : [ "asdx" ] }
{ "Name" : "Another", "Timestamp" : 1381234763188, "URL" : "http://ya.ru", "_id" : "qAB46c38hEFSw3NTE", "tags" : [ "one", "two" ] }
字段"标签"具有数组类型。
我有一个 HTML 模板来迭代书签:
<template name="bookmarks">
{{#each bookmarks}}
<a href="{{URL}}" class="bookmark_url">{{Name}}</a>
<div> {{tags}} </div>
{{/each}}
</template>
我想将每个标签显示到一个单独的"div"中。 像这样:
<template name="bookmarks">
{{#each bookmarks}}
<a href="{{URL}}" class="bookmark_url">{{Name}}</a>
{{#each tags}}
<div> {{current_tag}} </div>
{{/each}}
{{/each}}
</template>
我该怎么做?
谢谢。
遍历简单数组时,使用 {{this}}
访问每次迭代的值。
{{#each tags}}
<div>{{this}}</div>
{{/each}}
我建议将tags
数组转换为对象数组,例如:
"tags": [{_id: "1", "somevalue"}, {_id: "2", "someothervalue"}]
Spark 呈现引擎能够更好地决定在涉及唯一 ID 时何时重绘#each
的内容。参考: http://fogofcode.wordpress.com/2013/03/26/on-the-importance-of-_id-when-iterating-with-each-in-handlebarsmeteor/
{{#each current_tag in tags}}
<div> {{current_tag}} </div>
{{/each}}
相关文章:
- 如何在 for 循环中访问 json 数组字段的名称
- 如何通过向用户推送模式引用来更新用户对象's数组字段
- 访问嵌套的 json 数组字段
- PHP 从“选择 - 数组”字段中填充文本框
- 在 Rally SDK 2 中,如何更新数组字段,例如在变更集的工件上
- 遍历 html 模板中的数组字段
- 如何通过按数组字段选择来检索MongoDB文档
- Jquery 添加未传递给 php 的输入数组字段
- 如何在JSON数组字段的基础上从主干集合中提取模型
- 引用到另一个类的数组字段数组字段没有't刷新
- PHP中的HTML数组字段解析
- mongoose.js:我如何从引用的子数组中删除一个属性,同时使用填充和使用其中一个子数组字段作为过滤器
- js serialize()可以处理数组字段名吗?
- 在数组字段上展平嵌套的对象数组
- 如何在mongodb的子文档模式结构中更新数组字段
- 数组字段的弹性搜索日期范围过滤器
- 当数组字段为空时,替换值不能正常工作
- 如何从dc.js / crossfilter / d3中的数组字段中选择元素,并将每个元素用作图表的单独数据点
- 当我从模型中的数组字段中添加或删除对象时,Ember不更新模板
- 从javascript中设置postgres数组字段为null