问题描述
我有一个称为his-service的聚合物元素:
I have a polymer element called his-service:
<polymer-element name="gis-service" attributes="response url">
<template>
<style>
</style>
<core-ajax id="ajax"
auto
url="{{url}}"
method="get"
contentType = 'application/json'
on-core-response="{{postsLoaded}}"
body="{{body}}"
handleAs="xml">
</core-ajax>
</template>
<script>
Polymer('gis-service', {
created: function() {
this.response = [];
},
postsLoaded: function() {
this.response = [];
this.labels = [];
this.coordinates = [];
x = this.$.ajax.response.getElementsByTagName("CustomerServiceCenterData");
for (i=0;i<x.length;i++) {
if (x[i].getElementsByTagName("language")[0].innerHTML == "EN")
{
this.labels[i] = x[i].getElementsByTagName("label")[0].innerHTML;
this.coordinates.push({
lat:x[i].getElementsByTagName("lat")[0].innerHTML,
lng:x[i].getElementsByTagName("lng")[0].innerHTML
})
}
}
console.log(this.coordinates);
}
});
</script>
</polymer-element>
在索引文件中,我尝试访问对象标签和坐标.以下是索引的一部分:
In the index file, I try to access the object labels and coordinates. The following is part of the index:
<gis-service id="gservice" response="{{labels}}" url="someUrl">
</gis-service>
<script>
var gis_service = document.querySelector('gis-service');
console.log(gis_service);
</script>
如您所见,我正在尝试通过querySelector访问标签和坐标.但是,当我尝试通过以下方式获取标签时:
As you can see, I am trying to access labels and coordinates through querySelector. However, When I try to get labels for instance via:
gis_service.labels
它给了我不确定的信息.变量坐标也一样.我可以在执行时看到两个变量:console.log(gis_service),但无法访问它们.感谢您的帮助.
It gives me undefined. The same thing with the variable coordinates.I can see the two variables when I do: console.log(gis_service), but cannot access them.Any help is appreciated.
推荐答案
还有其他方法可以尝试访问值,请尝试var yourElement = document.getElementById('gservice');
如果这不起作用,请尝试使用"querySelector",如下所示:
There is other ways you can try to access the values, tryvar yourElement = document.getElementById('gservice');
if This doesn't work, try with 'querySelector' as shown below:
<gis-service id="gservice" class="gservice" response="{{labels}}" url="someUrl"> </gis-service> <script> var gis_service = document.querySelector('gservice'); console.log(gis_service); </script>
<gis-service id="gservice" class="gservice" response="{{labels}}" url="someUrl"> </gis-service> <script> var gis_service = document.querySelector('gservice'); console.log(gis_service); </script>
请参见其他类属性.
我也建议您升级到Polymer 1.0,因为它允许更健壮的方式进行操作...
Also i recommend you to upgrade to Polymer 1.0, because it allows more robust ways to do things...
这篇关于聚合物:访问聚合物元素对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!