因此,环顾四周并测试一些东西,我可以轻松地遍历数组或列表。
<template is="dom-repeat" items="{{inputs}}">
<paper-input label={{item}} id={{item}} style="width:5em"></paper- input>
</template>然而,我在遍历一组观察结果时遇到了问题。
<iron-data-table id="outputtable" items="{{output}}">
<template is="dom-repeat" items="[[output]]">
<data-table-column name="[[item.key]]">
<template>[[item.value]]</template>
</data-table-column>
</template>
</iron-data-table> 否则,这段代码可能不正确,但我无法克服错误。
聚合物::属性:不能将数组解码为JSON
在我迭代字符串数组的第一个例子中,我实际上想遍历一个对象数组,但一直无法这样做。
[{"uri":"/emoji/1"},{"uri":"/emoji/2"}]我的对象数组的简单示例。
<iron-ajax id="ajaxPost" url={{url}} handle-as="json" content-type="application/json" method="POST" on-response=clearAndOutput > </iron-ajax>
this.output [Object { uri="/emoji/1"}, Object { uri="/emoji/2"}, Object { uri="/emoji/3"}, 35 more...]
clearAndOutput: function(data) {
this.returnvalue = data.detail.response;
}发布于 2016-11-01 12:08:25
在dom-repeat中,items数组的每个数组元素都可以被中继器模板中的item访问(尽管您可以选择不同的迭代器名称)。如果名为"output"的数组包含一个"uri"属性,您将在模板中迭代该数组,如下所示:
<template is="dom-repeat" items="[[output]]">
<div>[[item.uri]]</div>
</template>
HTMLImports.whenReady(() => {
Polymer({
is: 'x-foo',
properties: {
output: {
type: Array,
value: () => [{"uri":"/emoji/1"},{"uri":"/emoji/2"}]
}
}
});
});<head>
<base href="https://polygit.org/polymer+1.7.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<template is="dom-repeat" items="[[output]]">
<div>[[item.uri]]</div>
</template>
</template>
</dom-module>
</body>
码页
您所看到的错误表明,"output"实际上不是一个数组,因此您必须调试它(或者提供关于如何设置它的附加信息,以帮助排除故障)。
https://stackoverflow.com/questions/40359531
复制相似问题