【问题标题】:Polymer 1.0 - Increment through nested array on template bind.Polymer 1.0 - 通过模板绑定上的嵌套数组递增。
【发布时间】:2016-01-05 12:16:16
【问题描述】:

我正在研究从 Web 服务接收 json 数组的聚合物元素。在组件中,有一个模板应该遍历数组以显示数组值之一。

以下代码显示其中一个嵌套数组。

    <template is="dom-repeat" items="{{response.0.sentence}}">
     <div style="display: inline; postition: realative;">
       <span>{{item.word}}></span>
     </div>
   </template>

这是 web 服务返回的数据示例

    [
{
    "sentence": [
        {
            "word": "He",
            "Color": "Black"   
        },
        {
            "word": "is",
            "color": "purple"   
        },
        {
            "word": "it",
            "color": "green"
        },
        {
            "word": "he",
            "color": "red"
        }
    ]
},
{
    "sentence": [
                {
            "word": "they",
            "Color": "Black"   
        },
        {
            "word": "it",
            "color": "purple"   
        },
        {
            "word": "polymer",
            "color": "green"
        },
        {
            "word": "red",
            "color": "green"
        }
    ]
}]

当前输出:他就是他

我需要做的是遍历对象中的每个句子并显示其 word 属性。通过增加 items="{{response.0.sentence}}"

中的 0 应该可以做到这一点

想要的输出:他是他,他是他,他们是聚合物红

我查看了几种潜在的解决方案,例如通过以下方式创建绑定,但没有运气。

   <template is="dom-repeat" items="{{response}}" index-as="i">
     <div style="display: inline; postition: realative;">
       <span>{{item.i.sentence.word}}></span>
     </div>
   </template>

我的下一个猜测是创建某种计算值,但我很难找到一种在绑定中实现它的方法,或者我在单步执行嵌套数组时遗漏了一些明显的东西。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript json data-binding polymer polymer-1.0


    【解决方案1】:

    使用嵌套的dom-repeat

     <template is="dom-repeat" items="[[response]]">
      <template is="dom-repeat" items="[[item.sentence]]">
        <span>[[item.word]]</span>
      </template>
     </template>
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多