私はこれら2つのコンポーネントをAngular2に、そしてchecklist
セレクタのテンプレートを持っています。EventEmitterオブジェクトが指定されたインスタンスを発行しないのはなぜですか?
import {Component, EventEmitter} from '@angular/core';
export class ToDoItem{
toDo: string;
checked: boolean;
constructor(toDo:string, checked: boolean){
this.toDo = toDo;
this.checked = checked;
}
}
@Component({
selector: 'to-do',
inputs: ['toDoItem'],
outputs: ['onToDoItemSelected'],
styleUrls: ['./app.checklist.css'],
template: `
<input type="checkbox"><label class="strikethrough">{{toDoItem.toDo}}</label><button (click)="itemClicked()">Delete</button>
`
})
export class ToDoItemComponent{
toDoItem : ToDoItem;
onToDoItemSelected: EventEmitter<ToDoItem>;
constructor(){
this.onToDoItemSelected = new EventEmitter();
}
itemClicked() : void {
console.log(this.toDoItem);
this.onToDoItemSelected.emit(this.toDoItem);
}
}
@Component({
selector: 'checklist',
templateUrl: './app.checklist.html'
})
export class CheckList{
toDoItems: Array<ToDoItem>;
constructor(){
this.toDoItems = [];
}
createToDo(description: string) : void {
if (description !== ""){
this.toDoItems.push(new ToDoItem(description, false));
}
}
removeToDoItem(toDoItem: ToDoItem) : void {
console.log(toDoItem);
var indice = this.toDoItems.indexOf(toDoItem);
this.toDoItems.splice(indice, 1);
}
}
そして、これはクラスCheckList
用のテンプレートファイルです:
まず、これら2つのコンポーネントである
<h2>A Checklist</h2>
<span *ngIf="toDoItems.length == 0">
There are not tasks yet...
</span>
<ul>
<li *ngFor="let toDoItem of toDoItems">
<to-do [toDoItem]="toDoItem" (itemClicked)="removeToDoItem(toDoItem)"></to-do>
</li>
</ul>
<hr>
<input type="text" placeholder="Enter the description..." #toDoDescription>
<button (click)="createToDo(toDoDescription.value); toDoDescription.value = '' ">Add</button>
基本的には、問題は、それがアイテムを削除しないことから構成されています。 EventEmitter
は暗黙のうちにremoveToDoItem
を呼び出しません。
いくつかの提案、アイデア、..それを解決するには?
'removeToDoItem(...)'の3行目の後に 'indice'にはどのような値がありますか? –
'this.toDoItems.splice(indice、1);'配列toDoItemsから指定された項目を削除します。 'console.log'は何も出力しません。 – InfZero
この例は、期待どおりに動作しますhttp://pastebin.com/91Dd54mL。私は質問としてコードを定義するモデルとしてモデルを使用しました。 – InfZero