Javascript 将iron ajax请求返回的值存储在变量中(聚合物)
我第一次使用ajax元素时遇到了一个问题,我认为这个问题很容易解决,但我无法在线找到解决方案。Javascript 将iron ajax请求返回的值存储在变量中(聚合物),javascript,json,attributes,polymer,polymer-1.0,Javascript,Json,Attributes,Polymer,Polymer 1.0,我第一次使用ajax元素时遇到了一个问题,我认为这个问题很容易解决,但我无法在线找到解决方案。 我使用iron ajax元素检索用户详细信息,并使用dom repeat循环。用户的名字和姓氏将被放置在我为显示用户而创建的自定义卡片元素中。一切正常,只是我无法将返回的id值存储在变量中。 我确信使用数据绑定可以做到这一点,但我无法让它工作。即使我在JavaScript中尝试使用getAttribute获取值,它也只返回null。 在下面的代码中可以找到我的iron ajax请求和响应,包括我笨拙地
我使用iron ajax元素检索用户详细信息,并使用dom repeat循环。用户的名字和姓氏将被放置在我为显示用户而创建的自定义卡片元素中。一切正常,只是我无法将返回的id值存储在变量中。 我确信使用数据绑定可以做到这一点,但我无法让它工作。即使我在JavaScript中尝试使用getAttribute获取值,它也只返回null。
在下面的代码中可以找到我的iron ajax请求和响应,包括我笨拙地尝试获取id值{{item.id}}。为此,当用户单击其中一张卡时,我创建了一个点击函数,然后该函数将获取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>
:主持人{
显示:块;
}
聚合物({
是‘我的学生观’,
特性:{
stdId:{
通知:正确,
}
},
//按字母顺序对数组/数据进行排序
排序数据:函数(a,b){
如果(a.first_nameb.first_name)返回1;
返回0;
},
//尝试获取学生卡的id值
sendId:function(){
var Idproperty=此。$$(“#学生卡”);
var idValue=idproperty.getAttribute('stdId');
log('id为:'+idValue);
},
});
重写sendId函数:
sendId: function(event) {
var idValue = event.model.item.id;
console.log('the id is:' + idValue);
},
非常感谢@Patricklaf!!!是否有任何文档可以解释为什么我只能通过event.model访问它??文档在这里: