将来自铁ajax请求的返回值存储在变量中(Polymer)
Store returned values from the iron-ajax request in variables (Polymer)
我第一次使用Polymers iron-ajax元素,遇到了一个问题,我认为这个问题很容易解决,但我无法在网上找到解决方案。
我使用iron-ajax元素检索用户详细信息,并用dom-repeat遍历它。用户的姓和名被放置在我创建的用于显示用户的自定义card元素中。除了无法将返回的id值存储在变量中之外,一切正常。我确信这是可能的使用数据绑定,但我不能让它工作。甚至当我尝试在JavaScript中使用getAttribute获取值时它也只返回null
在我的铁ajax请求下面的代码&可以找到响应,包括我笨拙地尝试获取id值{{item.id}}。为此,我创建了一个on-tap函数,当用户单击其中一张卡片时,该函数应该获得stdId属性的值。
<dom-module id="my-students-view">
<template>
<style>
:host {
display: block;
}
</style>
<!-- Iron-Ajax created connection and gets data -->
<iron-ajax
auto
id="requestRepos"
url="http://api.dev/user/"
handle-as="json"
loading="{{isloading}}"
last-response="{{response}}"></iron-ajax>
<!-- dom-repeat iterates the response -->
<template is="dom-repeat" items="[[response.data]]" sort="sortData">
<student-card
id="studentCard"
to="http://localhost:8080/profile-view/{{item.id}}"
picsrc="../../images/anonymous.jpg"
name="{{item.first_name}} {{item.last_name}}"
stdId="{{item.id}}"
on-tap="sendId"></student-card>
</template>
</template>
<script>
Polymer({
is: 'my-students-view',
properties: {
stdId: {
notify: true,
}
},
//Sort the array/data alphabetically
sortData: function(a, b) {
if(a.first_name < b.first_name) return -1;
if(a.first_name > b.first_name) return 1;
return 0;
},
//Try to get the id value of a the student card
sendId: function() {
var idPropertie = this.$$("#studentCard");
var idValue = idPropertie.getAttribute('stdId');
console.log('the id is:' + idValue);
},
});
</script>
</dom-module>
重写sendId函数:
sendId: function(event) {
var idValue = event.model.item.id;
console.log('the id is:' + idValue);
},
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 值对象在某个变量发生更改后发生更改
- 可以't使用Polymer's的核心ajax
- Javascript变量赋值|
- AngularJS-在JSON选择器中使用变量名
- Javascript计数器变量未显示正确的值
- 在Polymer 1.0中实现全局变量
- 如何将AngularJS变量绑定为一个Polymer全局变量
- 将来自铁ajax请求的返回值存储在变量中(Polymer)
- 如何使用Polymer 1.0 dom-bind公开局部变量