如何在点击按钮时填充聚合物下拉菜单?

时间:2015-03-12 13:04:05

标签: javascript html polymer

我的HTML页面中有一个polymer-dropdown-menu元素,如下所示:

   <script src="http://www.polymer-project.org/platform.js"></script>
<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
<link rel="import" href="http://www.polymer-project.org/components/paper-button/paper-button.html">
<link rel="import" href="http://www.polymer-project.org/components/paper-dropdown-menu/paper-dropdown-menu.html">
    <link rel="import" href="http://www.polymer-project.org/components/paper-dropdown/paper-dropdown.html">
 <link rel="import" href="http://www.polymer-project.org/components/core-menu/core-menu.html">  
 <polymer-element name="sample">
     <template>
         <div>
<paper-dropdown-menu id="Combo" label="Process">
    <paper-dropdown class="dropdown">
        <core-menu class="menu">
            <template repeat="{{processlist}}">
                <paper-item>{{}}</paper-item>
            </template>
        </core-menu>
    </paper-dropdown>
</paper-dropdown-menu>
         </div>
         <div>
             <paper-button id="start_button" raised>Start</paper-button>   
         </div>         
         </template>

  <script>
  Polymer({
    created: function() {    
    },
    domReady: function() {
      this.processlist = ["hi","hello"];                   
      var self = this;
      this.$.start_button.addEventListener('click', function () {  
       this.processlist=["hey","how","are","you"];        
      });
      });
    }
  }); 
  </script>
     </polymer-element>

我需要点击按钮填充进程列表。我在我的按钮的事件监听器函数中编写代码,该函数更新了进程列表,但这不起作用。

我能够显示静态数组值,但不能动态点击按钮。

任何帮助将不胜感激。我是HTML,JavaScript,当然还有聚合物的新手。

1 个答案:

答案 0 :(得分:1)

也许我错了,但看起来你在处理程序中有不正确的“this”上下文。 There我创建了相同的代码片段,通过点击按钮显示生成的列表,试一试。

<script src="https://cdnjs.cloudflare.com/ajax/libs/polymer/0.5.5/polymer.min.js"></script>
<polymer-element name="my-element" constructor="" attributes="">
    <template>
        <paper-button label="Submit" on-tap="{{onInvoke}}" affirmative autofocus>Populate list</paper-button>
        <br/>
        <paper-dropdown-menu id="Combo" label="Process">
            <paper-dropdown class="dropdown">
                <core-menu class="menu">
                    <template repeat="{{processlist}}">
                        <paper-item>{{}}</paper-item>
                    </template>
                </core-menu>
            </paper-dropdown>
        </paper-dropdown-menu>
    </template>
    <script>
        Polymer('my-element', {
            onInvoke: function() {
                this.processlist = ['a', 'b'];
            }
        });
    </script>
</polymer-element>
<my-element></my-element>