【问题标题】:Polymer 1.0 :Nested dom-repeat templates cannot show the content of a child objectPolymer 1.0:嵌套的 dom-repeat 模板无法显示子对象的内容
【发布时间】:2015-06-22 13:30:34
【问题描述】:

我想在帖子数据中显示一个对象内容(cmets)(使用 firebase-collection): 这是firebase中帖子的结构:

"Publication" : {
       "date":"22-06-2015",
 "content" : "post example",
 "comments": {
  //a post has too many comments on it
  }
}

我的目标是添加另一个 dom-repeat 以显示 cmets,但我什么都看不到。 这是代码示例(我提到的第一个模板可以正常工作)

<template is="dom-repeat" items="{{posts}}" as="post">
      <!-- Content here is appearing correctly --> 
          <template is="dom-repeat" items="{{post.comments}}" as="commentaire">
               <span>{{commentaire.date}}</span>
           </template>
</tempalte>

我遵循了聚合物迁移文档但没有结果,如果有任何解决方案,我将不胜感激。

【问题讨论】:

    标签: polymer polymer-1.0


    【解决方案1】:

    我认为您的 cmets 属性需要是一个数组才能正确绑定。

    【讨论】:

    • 嗯,这是真正的问题,我无法管理这个“post.cmets”,甚至无法将其转换为数组,所以我只能使用 JavaScript 来解决这个问题。这是与从 firebase-element(在 Polymer 的 0.5 版本中)到 1.0 的新迁移相关的问题。
    • 你可以像arrayify: function(obj) { return Object.keys(obj).map(function(k) { return obj[k]; }) }这样写一个过滤器,然后绑定到items="{{arrayify(post.comments)}}"
    • @ScottMiles 为什么不Object.values(obj)
    • @milan 不好supported
    【解决方案2】:

    你可以不带'as'标签试试这个。

    <template is="dom-repeat" items="{{posts}}">
          <template is="dom-repeat" items="{{item.comments}}">
               <span>{{item.date}}</span>
           </template>
    </tempalte>
    

    我的理解是“项目”是聚合物的一个内部术语,它代表一个数组/对象。我可能不完全正确,但这个技巧对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多