2016-08-01 5 views
2

は、このホスト・コンポーネントのコードを想像してみ?コールネストされたコンポーネントの機能angular2

+0

もっとコードを入力してください。 'save()'と 'waitingForHostComp()'はどこにありますか? –

+0

'save()'はChildComponentをインポートするParentComponentにあります。 'waitingForHostComp()'はChildComponent上にあります。基本的に私が望むのは、ParentComponent(ChildComponentを注入する)がサーバーからの応答を取得したときにトリガーされるChildComponentのリスナーです。 – nottinhill

答えて

6

できることは、ViewChildを使用しています。つまり、子コンポーネントを親にViewChildとして挿入します。

import { ViewChild } from '@angular/core'; 
import { Component } from '@angular/core'; 
import { ChildComponent } from './child.component'; 

@Component({ 
    selector: 'parent-component', 
    template: ` implements AfterViewInit 
    <child-component></child-component> 
    `, 
    directives: [ChildComponent] 
}) 
export class ParentComponent { 
    @ViewChild(ChildComponent) 
    private childComponent: ChildComponent; 

    save(): void { 
     this.myService.myHTTPCall().subscribe((event) => { 
      this.childComponent.waitingForHostedComp(event); 
     }) 
    } 

} 

Component Interaction Cookbook: Parent Calls a ViewChildで詳細を確認できます。

+0

おもしろいことに、私の他のインプットとアウトプットは以前と同じように機能します(私はこのコードサンプルで引用しませんでした)? – nottinhill

+1

もちろん、以前と同じように動作します。 ViewChildは、typescriptコード内の子コンポーネントとやり取りする単なる方法です。 –

+0

私は 'private childComponent:ChildComponent;'をコンストラクタに入れなければなりませんでした。そしてそれは完全に動作します。ありがとう、ベルナルド。 – nottinhill

関連する問題