2017-09-03 9 views
0

Angular2 +の場合、@outputを使用して親に通知することができます。子イベントが発行される前または後に呼び出される親関数を子供に知らせる方法はありますか?Angular2 +親が親から通知を受け取る方法

以下は親HTMLコードで、子コンポーネントを呼び出してonClickまたはOnClose関数を実行します。

<child-comp (test1)="onClick()"> 
    ... 
</child-comp> 

<child-comp (test2)="onClose()"> 
    ... 
</child-comp> 

以下は、子コードです。

@Component({ 
    selector: 'child-comp' 
}) 

export class ChildComponent{ 
    @Output() test1: EventEmitter<any> = new EventEmitter(); 
    @Output() test2: EventEmitter<any> = new EventEmitter(); 

    handleClick() { 
     //this.test1.emit("test1 is emitted"); 
     //or 
     //this.test2.emit("test2 is emitted"); 

    } 
} 

質問は、両方のtest1の中で、TEST2は、一度TEST1またはTEST2イベントが放出され、どのように子供が実行される親機能知らせるためにhandleClick機能していますか?

+0

を?実際に解決しようとしている問題は何ですか?あなたは両方を放出することができ、親がどちらを使用するかを決定します。 –

+0

イベントエミッターと混同していると思いますが、この[リンク](https://rahulrsingh09.github.io/AngularConcepts/inout)をチェックしてください。 –

答えて

0

は子供に@Input()プロパティwhatHappendInParentを追加して、このような何か書く:あなたは親が `test1`または` test2`に加入しているかどうかを知りたい

<child-comp (test1)="onClick()" [whatHappenedInParent] = whatHappened> 
    ... 
</child-comp> 

<child-comp (test2)="onClose()" [whatHappenedInParent] = whatHappened> 
    ... 
</child-comp> 
... 
export class myParent { 
    whatHappened: string; 
    onClick(){ 
    this.whatHappened = "onClick was called"; 
    } 
    onClose(){ 
    this.whatHappened = "onClose was called"; 
    } 
} 
関連する問題