<template is="dom-bind">
<iron-ajax
auto
url="http://localhost:9000/api/version"
last-response="{{versionNumber}}"
verbose
></iron-ajax>
<template is="dom-repeat" items="{{versionNumber}}">
<small class="u-ml+">{{item.first}}</small>
</template>
<template>
<small>[[versionNumber]]</small>
</template>
</template>我对聚合物有点迷惑-我有一个iron-ajax元素,它被设置为与API端点对话,它返回我的应用程序的当前版本。
我希望能够将这个版本号直接绑定在页面上。上面的代码中有什么地方我做得不正确吗?
我尝试使用dom-repeat模板并尝试获取第一个项目,但似乎什么也得不到。与尝试在<small>标记内进行单向绑定相同。
我的理解是,如果我在dom-bind模板中,就不必定义自定义元素。
发布于 2016-09-07 05:52:07
是的,数据绑定可以在dom-bind模板中工作,而不需要自定义元素。
代码中的一个问题是<small>周围的template标记
<template>
<small>[[versionNumber]]</small>
</template>模板的内容本身不会在DOM中显示/呈现。有关模板的一些详细信息,请参阅http://www.html5rocks.com/en/tutorials/webcomponents/template/ )。
在dom-bind模板中使用带有额外模板标记的<small>[[versionNumber]]</small>应该是可行的。
另一个问题是,默认情况下,iron-ajax以JSON的形式处理响应,因此当它接收到字符串时,可能会遇到解析错误,而last-response将不会获得任何值。
您必须相应地指定iron-ajax的handleAs属性。
<iron-ajax handle-as="text" ...>
而dom-repeat只适用于数组。
https://stackoverflow.com/questions/39358113
复制相似问题