我正在尝试在 Polymer 中创建一个简单的待办事项列表,并且添加部分正在工作。当我单击项目旁边的“删除”图标时,它会删除最近添加的项目,而不是应该删除的项目。看起来它得到了错误的数组索引,我不知道如何修复它?谢谢!
<dom-module id="my-todo-list">
<template>
<div class="card">
<div class="form">
<paper-input label="Task" value="{{todo.task}}"></paper-input>
<paper-button raised on-tap="_addTodo">Add Todo</paper-button>
</div>
<template is="dom-repeat" items="{{todos}}">
<paper-card class="todos">
<paper-checkbox id="checkTodo" on-click="_completeTodo"></paper-checkbox>
<p>{{item.task}}</p>
<iron-icon icon="delete" on-tap="_destroyAction"></iron-icon>
</paper-card>
</template>
</div>
</template>
<script>
Polymer({
is: 'my-todo-list',
properties: {
todo: {
type: Object,
value: function() {
return {};
}
},
todos: {
type: Array,
value: function() {
return [];
}
}
},
_addTodo: function() {
console.log(this.todo);
this.push('todos', this.todo);
// this.todo = {};
},
_destroyAction: function(todo) {
var index = this.todos.indexOf(todo);
this.splice('todos', index, 1);
},
});
</script>
</dom-module>