简体   繁体   English

聚合物:访问聚合物元素对象

[英]Polymer: access polymer element objects

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>

As you can see, I am trying to access labels and coordinates through querySelector. 如您所见,我正在尝试通过querySelector访问标签和坐标。 However, When I try to get labels for instance via: 但是,当我尝试通过以下方式获取标签时:

gis_service.labels

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. 我可以在执行时看到两个变量:console.log(gis_service),但无法访问它们。 Any help is appreciated. 任何帮助表示赞赏。

Define it outside of the function and then you should be able to use it externally. 在函数外部定义它,然后您应该可以在外部使用它。 This format will work for the .8 version. 此格式适用于.8版本。 1.0 version will need to use the new format. 1.0版将需要使用新格式。

 created: function() { this.response = []; }, labels: undefined, postsLoaded: function() { 

You should read about databinding here 您应该在这里阅读有关数据绑定的信息

Don't do this: 不要这样做:

 <core-ajax id="ajax"
      auto
      url="{{url}}"
      method="get"
      contentType = 'application/json'
      on-core-response="{{postsLoaded}}"
      body="{{body}}"
      handleAs="xml">
    </core-ajax>

Do this instead: 改为这样做:

 <core-ajax id="ajax" auto url="{{url}}" method="get" contentType ='application/json' on-core-response="{{postsLoaded}}" body="{{body}}" handleAs="xml"></core-ajax>

Putting the bindings on different lines isnt supported yet 尚不支持将绑定放在不同的行上

There is other ways you can try to access the values, try var yourElement = document.getElementById('gservice'); 还有其他方法可以尝试访问值,请尝试var yourElement = document.getElementById('gservice'); if This doesn't work, try with 'querySelector' as shown below: 如果这不起作用,请尝试使用“ querySelector”,如下所示:

<gis-service id="gservice" class="gservice" response="{{labels}}" url="someUrl"> </gis-service> <script> var gis_service = document.querySelector('gservice'); console.log(gis_service); </script>

See the additional class attribute. 请参阅其他类属性。

Also i recommend you to upgrade to Polymer 1.0, because it allows more robust ways to do things... 我也建议您升级到Polymer 1.0,因为它允许使用更强大的方法来做事...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM