5

如何访问函数内属性的值?这里是楼盘

properties:{
  name: {type: String}
}

<my-element name="Subrata"></my-element>

在里面<my-element>我有一个这样的功能:

方法#1

<dom-module id="my-element">
  <template>
    ...
    ...
  </template>

  <script>
  (function () {
    is: 'my-element',
    properties:{
      name: {type: String}
    },
  
    getMyName: function(){
      return this.name;
    }
  })();
  </script>
</dom-module>

我的另一种方法是将值放入元素中,但这也不起作用。

方法#2

<dom-module id="my-element">
  <template>
    <!-- value of name is rendered OK on the page -->
    <p id="maxp">{{name}}</p>
  </template>
  
  <script>
    (function() {
      is: 'my-element',
      properties: {
        name: {type: String}
      },
      getMyName: function(){
          var maxpValue = this.$$("#maxp").innerHTML;
          return  maxpValue;
      }
    })();
  </script>
</dom-module>

我怎样才能做到这一点?请帮忙。

提前致谢

4

2 回答 2

3

您应该使用该函数,而不是使用自调用匿名Polymer函数。更改(function() { ... })();您的代码以读取Polymer({ ... });.

这是一个例子:

<dom-module id="my-element">
  <template>
    ...
  </template>
</dom-module>

<script>
  Polymer({
    is: 'my-element',

    properties: {
      name: {
        type: String
      }
    },

    getMyName: function() {
      return this.name;
    }
  });
</script>

我建议您遵循 Polymer 文档中的入门指南,因为它涵盖了所有这些以及更多内容。当您希望开始使用 Polymer 时,这是一个很好的起点。

于 2015-06-10T12:44:49.983 回答
2

你可以简单地做

this.name

访问变量的任何函数中的任何位置

于 2016-11-30T15:37:51.770 回答