【发布时间】:2015-08-18 03:16:29
【问题描述】:
我在 Polymer 1.0.2 中遇到了 Iron-ajax 和数据绑定问题。即使是稍加改动的example from the Polymer documentation 也不起作用。
这是我的更改代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/iron-ajax/iron-ajax.html">
</head>
<body>
<template is="dom-bind">
<iron-ajax
auto
url="http://jsonplaceholder.typicode.com/posts/"
lastResponse="{{data}}"
handleAs="json">
</iron-ajax>
<template is="dom-repeat" items="{{data}}">
<div><span>{{item.id}}</span></div>
</template>
</template>
<script>
(function (document) {
'use strict';
var app = document.querySelector('#app');
window.addEventListener('WebComponentsReady', function() {
var ironAjax = document.querySelector('iron-ajax');
ironAjax.addEventListener('response', function() {
console.log(ironAjax.lastResponse[0].id);
});
ironAjax.generateRequest();
});
})(document);
</script>
</body>
</html>
我所做的只是输入一个 URL 以获得真正的 JSON 响应并设置 auto 和 handleAs 属性。我还为响应事件添加了一个带有侦听器的小脚本。侦听器工作正常并处理响应,但 dom-repeat 模板中的跨度未呈现。
我正在使用 Polymer 1.0.2 和铁元素 1.0.0
【问题讨论】:
-
我遇到了类似的问题,请查看here 我的问题和两个答案(包括我自己的!),看看它们是否有帮助。