簡體   English   中英

向事件處理函數添加參數?

[英]Add Argument to event-handler function?

我正在與Polymer一起在一個小型Web項目上工作。

我正在為項目列表中的每個項目顯示一個刪除按鈕。 刪除按鈕觸發deleteItem()函數。 我想添加item.iditem本身作為參數,以便刪除正確的item。

我怎樣才能做到這一點?

<template id="bind" is="dom-bind">
  <script>
    var bind = document.querySelector('#bind');
    bind.deleteItem = function() {
      // Get item id?
    }
  </script>

  <template is="dom-repeat" items="{{data}}">
    <span>{{item.name}}</span>
    <paper-button on-click="deleteItem" id="{{item.id}}">Delete</paper-button></p>
  </template>
</template>

您不能將其他參數傳遞給事件處理程序,但是可以獲取對event.model模型的event.model

有關示例,請參見https://www.polymer-project.org/1.0/docs/devguide/templates.html#handling-events

<dom-module id="simple-menu">

  <template>
    <template is="dom-repeat" id="menu" items="{{menuItems}}">
        <div>
          <span>{{item.name}}</span>
          <span>{{item.ordered}}</span> 
          <button on-click="order">Order</button>
        </div>
    </template>
  </template>

  <script>
    Polymer({
      is: 'simple-menu',
      ready: function() {
        this.menuItems = [
            { name: "Pizza", ordered: 0 },
            { name: "Pasta", ordered: 0 },
            { name: "Toast", ordered: 0 }
        ];
      },
      order: function(e) {
        var model = e.model; // <== get the model from the clicked item
        model.set('item.ordered', model.item.ordered+1);
      }
    });
  </script>

</dom-module>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM