2017-06-01 42 views
0

爲了得到我使用的輸入的變化以下從模板DOM重複

<template is="dom-repeat" items="{{employees}}"> 
    <div>First name: <input on-change="cambiado" value="{{item.first::change}}"></div> 
    <div>Last name: <input on-change="cambiado" value="{{item.last::change}}"></div> 
    </template> 
... 
<script> 
... 
cambiado(e){ 
    var datos=this.employees[e.model.__data.index] 
    setTimeout(function() { 
     console.log(datos.first+' '+datos.last) 
    }, 1000); 
} 
... 

</script> 

聚合物得到改變,但我相信,聚合物可以在easer方式獲取事件或剛剛得到的輸入改變。謝謝。

這與觀察員完整的元素不起作用:

<link rel="import" href="../bower_components/polymer/polymer.html"> 

<dom-module id='employee-list'> 
    <template> 
     <div>Employee List</div> 
     <p></p> 
     <template is="dom-repeat" items="{{employees}}"> 
      <div>First name: <input value="{{item.first}}"></div> 
      <div>Last name: <input value="{{item.last}}"></div> 
     </template> 
    </template> 

    <script> 
     class EmployeeList extends Polymer.Element { 
      static get is() { return 'employee-list' } 
      static get properties() { 
       return { 
        item: { 
         type: Object, 
         value: function() { 
          return {}; 
         } 
        } 
       } 
      } 
      static get observers() { 
       return [ 
        'itemChanged(item.*)' 
       ] 
      } 
      constructor() { 
       super() 
       this.employees = [ 
        { first: 'Bob', last: 'Li' }, 
        { first: 'Ayesha', last: 'Johnson' }, 
        { first: 'Fatma', last: 'Kumari' }, 
        { first: 'Tony', last: 'Morelli' } 
       ]; 
      } 

      itemChanged(first) { 
       if (first) { 
        console.log('new first: ' + first); 
       } else { 
        console.log('first is undefined'); 
       } 
      } 
     } 
     customElements.define(EmployeeList.is, EmployeeList) 
    </script> 
</dom-module> 

回答

2

您可以使用觀察者,所以當item對象的某些屬性得到改變命名的函數被調用:

observers: [ 
    'itemChanged(item.*, cambiado)' 
] 

Docs:https://www.polymer-project.org/1.0/docs/devguide/observers

+0

觀察到的函數只是在加載元素時觸發,而不是在輸入新值時觸發。可能我想念一些東西。 – Mbanolas

+0

選中此項,可能有所幫助:http://plnkr.co/edit/dkFohUbkTZbZiKBlU0to?p=preview –