2016-12-18 3 views
1

私はこれら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を呼び出しません。

いくつかの提案、アイデア、..それを解決するには?

+0

'removeToDoItem(...)'の3行目の後に 'indice'にはどのような値がありますか? –

+0

'this.toDoItems.splice(indice、1);'配列toDoItemsから指定された項目を削除します。 'console.log'は何も出力しません。 – InfZero

+0

この例は、期待どおりに動作しますhttp://pastebin.com/91Dd54mL。私は質問としてコードを定義するモデルとしてモデルを使用しました。 – InfZero

答えて

2

インポートファイルが間違っている意味しました。これを行うには、このようなEventEmitterを使用する必要があります。

import { Component, EventEmitter, Input, Output } from '@angular/core'; 

      @Component({ 
       selector: 'rio-counter', 
       templateUrl: 'app/counter.component.html' 
      }) 
      export class CounterComponent { 
       @Input() count = 0; 
       @Output() result = new EventEmitter<number>(); 

       increment() { 
       this.count++; 
       this.result.emit(this.count); 
       } 
      } 

子クラスのHTML

<div> 
    <p>Count: {{ count }}</p> 
    <button (click)="increment()">Increment</button> 
</div> 

親コンポーネントは次のようにする必要があります。

import { Component, OnChanges } from '@angular/core'; 

@Component({ 
    selector: 'rio-app', 
    templateUrl: 'app/app.component.html' 
}) 
export class AppComponent implements OnChanges { 
    num = 0; 
    parentCount = 0; 

    ngOnChanges(val: number) { 
    this.parentCount = val; 
    } 
} 

親HTML部分

<div> 
    Parent Num: {{ num }}<br /> 
    Parent Count: {{ parentCount }} 
    <rio-counter [count]="num" (result)="ngOnChanges($event)"> 
    </rio-counter> 
</div> 

例rangle.ioノートから取ら参照用。 @Output()onToDoItemSelectedを使用し、Outputを親コンポーネント呼び出し(onToDoItemSelected)= "removeToDoItem($ event)"にインポートします。私はこれが助けてくれることを願っています

+0

私はあなたが提案した変更を行いましたが、 'OnChange'は' angular/core'に属しません:https://www.dropbox.com/s/9t1yq7hqcb7wnql/Ejercicio-2-OnChange.png?dl=0 – InfZero

+0

@infZero私はあなたが古い角度の角度/コアパッケージを使用していると思います。 package.jsonファイルを表示できますか?実際にはonChangeはangle/coreに属します。ここにオフィシャルなリンクがありますhttps://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child-on-changes –

+0

'package.json':https:// www .dropbox.com/s/p00t31vkb67p4kk/package.json?dl = 0 – InfZero

0

正しい引数をremoveToDoItemに指定していないためです。あなたのCheckListテンプレートでは、持っている:EventEmitterそれは(click)となりますようにあなたemitは、実際に$eventデータを引数にして

(itemClicked)="removeToDoItem(toDoItem)" 

を。だからあなたのコードは次のようになります。他に変更を加えることなく、CheckListremoveToDoItemに正しいデータを渡す必要があります

(itemClicked)="removeToDoItem($event)" 

+0

私はそれを変更しましたが、それでも動作しません。 – InfZero

0

イベントまたは@Output() itemClickedがないため、(itemClicked)="removeToDoItem(toDoItem)"によってremoveToDoItem()が呼び出されません。

はおそらく、あなたは

(click)="itemClicked(toDoItem) 
+0

私は 'ToDoItemComponent'の宣言で' outputs:['onToDoItemSelected'] 'を指定しました。 – InfZero

+0

http://pastebin.com/91Dd54mL私はこの例に従っています。 – InfZero

+0

私はあなたのコードとpastebinの間に多くの類似点を見ることができません。 –

関連する問題