2016-11-28 78 views
-1

从这个铁的Ajax元素:聚合物嵌套DOM重复预期阵列错误

<iron-ajax 
    id="ajax" 
    url="..." 
    handle-as="json" 
    verbose=true 
    last-response={{ajaxResponse}} 
    loading="{{cargando}}"> 
</iron-ajax> 

我得到这个铁Ajax响应:

{ 
    "id": "3", 
    "idcontenido": "9", 
    "imagenes": ["oneimage.png", "anotherimage.png"], 
    "tipo_imagen": "img-circle", 
    "html": "Lorem ipsum" 
} 

,我需要实现一个嵌套的DOM重复结构以迭代来自imagenes属性的项目。这是我的代码:

<template is="dom-repeat" items="[[ajaxResponse]]" as="registro"> 
    <template is="dom-repeat" items="[[registro.imagenes]]" as="imagen"> 
     <img class="[[registro.tipo_imagen]]" src="img/[[imagen]]" alt="" width="140" height="140" /> 
    </template> 
</template> 

但我得到这个错误:

[dom-repeat::dom-repeat]: expected array for 'items', found Object {id: "3", idcontenido: "9", imagenes: Array[2], tipo_imagen: "img-circle", html: "Lorem ipsum"}

为什么? 谢谢!

+0

您得到的响应不是数组,它的对象和dom-repeat仅适用于数组 – a1626

+1

[\ [dom-repeat :: dom-repeat \]的可能重复:\ items \ ',找到对象](http://stackoverflow.com/questions/40824121/dom-repeatdom-repeat-expected-array-for-items-found-object) – a1626

回答

1

<dom-repeat>只能迭代array s,因此您无法将object s传递给它。您看到expected array错误消息,因为您要绑定<dom-repeat>.items到,这是一个对象。

first question,您的服务已发出的数组项的对象:

{ 
    "1": [{"id": "1"}, {"id": "2"}], 
    "2": [{"id": "3"}], 
    ... 
} 

而不是数组:

[ 
    [{"id": "1"}, {"id": "2"}], 
    [{"id": "3"}], 
    ... 
] 

在这个问题上,您的服务发送单个项目对象:

{"id": "1"} 

而不是阵列:

[{"id": "1"}] 

如果真的应该是一个单一的项目对象,你可以通过只删除外<dom-repeat>,直接绑定到ajaxResponse.imagenes解决您的模板:

<template is="dom-repeat" items="[[ajaxResponse.imagenes]]" as="imagen"> 
    <img class="[[registro.tipo_imagen]]" src="img/[[imagen]]" alt="" width="140" height="140" /> 
</template> 

在另一方面,如果是应该是一个数组,你需要使用我在first question中描述的技术来修复你的服务,或者在客户端进行转换。