我已经深入研究了聚合物的ajax核心元素,如下代码工作正常:
<core-ajax url="./ajax-test.txt" auto response="{{resp}}"></core-ajax>
<textarea value="{{resp}}"></textarea>
我可以从中获取价值{{resp}}
在这种情况下。我深入研究了 core-ajax 源代码并了解它是如何完成的:
- make
response
通过设置发布的属性attributes="response ..."
- 将 ajax 响应传递给
this.response
然后我尝试构建自己的ajax组件但没有成功,我的ajax组件代码是:
Polymer('louis-ajax', {
url: '',
response: null,
ready: function() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
this.response = xmlhttp.responseText;
}
}.bind(this);
xmlhttp.open("GET",this.url,true);
xmlhttp.send();
}
});
我的应用程序代码是这样的:
<louis-ajax url="http://polymer.snspay.cn/api/posts.json" response="{{response}}"></louis-ajax>
<span>We have got the ajax response as</span> : <input type='text' value="{{response}}" />
结果是ajax请求已经成功完成,但是输入的值是“{{response}}”,而不是{{response}}的值,所以我认为我对发布的属性工作的理解有问题,有什么帮助吗? Thk。
我知道你说过你已经弄清楚了,但对于其他来到此页面寻找完整工作解决方案和解释的人来说,这就是。
如果您想要数据绑定而不必创建自定义元素,则必须将代码放入模板中,并使用is
属性设置为auto-binding
:
<template is="auto-binding">
<core-ajax url="./ajax-test.txt" auto response="{{resp}}"></core-ajax>
<textarea value="{{resp}}"></textarea>
</template>
如果没有这个,Polymer 将不知道它需要连接 html 中的绑定,诸如此类{{resp}}
将被视为文本。
更详细的解释可以在这里找到:http://www.polymer-project.org/docs/polymer/databinding-advanced.html#autobinding http://www.polymer-project.org/docs/polymer/databinding-advanced.html#autobinding
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)