2017-09-14 2 views
0

ちょうどタイトルのように、私は特定の条件の後に子供のコンポーネント出力EventEmitterを聞いて停止する必要がありますが、私はそれをもう一度聞く必要があります。 それでは、どうすればいいですか? EventEmitterからsubscribe()およびunsubscribe()を使用して悪い習慣について読んだことがあります。子供のコンポーネントEventEmitterからのemitメソッドのリッスンを開始および終了できますか?Angular 2?

+0

それはあなたが細断処理サービスのために行く方が良い[リンク](https://rahulrsingh09.github.io/AngularConcepts/faq)質問2 –

+0

条件 'を来る' どこから?親または子供から? – Vega

+0

親からの@Vega – GroniumArgor

答えて

0

あなたは、新しい子の属性としてその条件を追加することができます。

親HTML

<child [conditionForChild]="myCondition" ......></child> 

親活字体

conditionForChild: boolean; // set it as needed afterwards 

子typescriptです:

@Input() conditionForChild; 
    .... 
    action($event) { //this is your existing method which will emit the event to the parent 
    if(conditionForChild){ 
     this.action.emit(); 
    } 
    } 

または:

<child (action)="condition? delete() : null" ......> </child> 
関連する問題