2016-07-14 11 views
1

親コンポーネント検出Angular2 - コンポーネントの子変数の変更

import { Component, OnChange } from '@angular/core'; 
import { ChildComponent } from '../../reusable/user-offers/user-offers.component'; 
@Component({ 
    ... 
    directives: [ ChildComponent ] 
}) 
export class ParentComponent implements OnChange { 
    @ViewChild(ChildComponent) 
    private child: ChildComponent; 

    ngOnChanges() { 
    detecting this.child.detectme variable... 
    } 

} 

子コンポーネント

import { Component, Input } from '@angular/core'; 
@Component({ 
    ... 
}) 
export class ParentComponent {  
    @Input() detectme: string; 
    //in view something changes detectme value (not important here) 
} 

がどのように私はchild.detectmeが変更された場合に検出し、それが価値だ印刷することができますか?

+0

これは 'ngOnChanges'ではなく' ngOnChange'です – Lekhnath

+0

申し訳ありませんが、これは手作業で書き直されたためです。私はngOnChangesを持っています。 – elzoy

答えて

2

親コンポーネントで、子コンポーネントの値が変更されたことを検出しますか?その場合は、子コンポーネントでEventEmitterを使用し、イベントを送出する必要があります。次に、(eventNameFromChild)="handler($event)"構文を使用して、親コンポーネントのイベントをサブスクライブします。

plunkが作成されました。

関連する問題