2017-08-08 15 views
1

私は子コンポーネント(カレンダー)と親コンポーネント(フォーム)を持っています。私はカレンダーで値の値を選択する必要があり、私はフォームコンポーネントの値にアクセスしたい。angle2の親コンポーネントに子コンポーネントの値を取得する方法。最善のアプローチは何ですか?

これを可能な限り最良の方法で達成するにはどうすればよいですか?

子供Component.ts:

import { 
 
    Component, 
 
    OnInit, 
 
    ViewChild, 
 
    Input 
 
} from '@angular/core'; 
 

 
@Component({ 
 
    selector: 'calendar', 
 
    template: ' 
 
    <md2-datepicker name="mindate" 
 
         placeholder="Minimum Date" 
 
         [(ngModel)]="minDate" 
 
         [mode]="mode" 
 
         [touchUi]="touch" 
 
         [format]="dateFormat" 
 
         #minDateControl="ngModel" [fromDate]="minDateControl"></md2-datepicker> 
 
     <md2-datepicker name="maxdate" 
 
         placeholder="Maximum Date" 
 
         [(ngModel)]="maxDate" 
 
         [min]="minDate" 
 
         [mode]="mode" 
 
         [touchUi]="touch" 
 
         [format]="dateFormat" 
 
         #maxDateControl="ngModel"></md2-datepicker> 
 
    ', 
 
}) 
 

 
export class CalendarComponent implements OnInit { 
 

 
    today: Date = new Date(); 
 

 

 
    minDate: Date; 
 
    maxDate: Date; 
 

 
    constructor(){ 
 

 
    } 
 

 
    ngOnInit(){ 
 

 
    } 
 

 
}

親コンポーネント:

import { 
 
    Component, 
 
    OnInit, 
 
    ViewChild, 
 
    Input 
 
} from '@angular/core'; 
 

 
import { Router } from '@angular/router'; 
 

 
import { ChartModule,ChartComponent } from 'angular2-chartjs'; 
 
import { ChartService } from './../shared/chart.service'; 
 

 
import { Colors, xAxisWidth } from './../shared/data'; 
 

 
@Component({ 
 
    selector: 'form-js', 
 
    template: ` 
 
    <h3 class="chartHeading">Parameter Selection Form</h3> 
 
    <calendar></calendar> 
 
    `, 
 
}) 
 

 
export class FormComponent implements OnInit { 
 

 

 
    @Input fromDate: string; 
 
    @Input toDate: string; 
 

 
    constructor(){ 
 

 
    } 
 

 
    ngOnInit(){ 
 

 
    } 
 

 
    alert(fromDate); 
 
}

私からとに達成することができますどのようにangle2のフォームコンポーネントの日付値?

+0

共有サービスまたは@ Input/@ Outputプロパティのいずれかを使用できます。後者はこの場合に最も適しており、Angular docs:https:// angularに詳しく記述されています。io/guide/component-interaction –

+0

提供されているサービスを利用して、データを渡すことができます。 – mast3rd3mon

答えて

1

親コンポーネントの子コンポーネントの値を取得する場合は、単に@ViewChildアノテーションを使用します。

@ViewChild('minDateControl') calendar: Md2Datepicker; 

よう

次に、あなたは、コンポーネントのすべてのパブリックリソースへのアクセスを持っています。

あなたが親コンポーネントに親コンポーネントと子コンポーネント

間の通信を容易にします@outputデコレータ、使用することができます

+0

親コンポーネントは@ViewChildデコレータを使用して子コンポーネントの要素を取得できますか? –

+0

これは間違った解決策です。@ViewChildを使用して親コンポーネントから子要素にアクセスすることはできません! –

+0

これは機能していません – VSK

3

:子コンポーネント

@Component({ 
    selector: 'form-js', 
    template: ` 
    <h3 class="chartHeading">Parameter Selection Form</h3> 
    <calendar (onSelectValue)='selectValue($event)'></calendar> 
    `, 
}) 
export class FormComponent{ 

    selectValue(newValue : any) { 
    console.log(newValue); 
    } 

} 

import { 
    Component, 
    OnInit, 
    ViewChild, 
    Input, 
    Output // add this import 
    EventEmitter // add this import to 
} from '@angular/core'; 

@Component({ 
    selector: 'calendar', 
    template: ' 
    <md2-datepicker name="mindate" 
         placeholder="Minimum Date" 
         [(ngModel)]="minDate" 
         [mode]="mode" 
         (change)='onChange()' // add this event listener 
         [touchUi]="touch" 
         [format]="dateFormat" 
         #minDateControl="ngModel" [fromDate]="minDateControl"></md2-datepicker> 
     <md2-datepicker name="maxdate" 
         placeholder="Maximum Date" 
         [(ngModel)]="maxDate" 
         [min]="minDate" 
         (change)='onChange()' //add this event listener 
         [mode]="mode" 
         [touchUi]="touch" 
         [format]="dateFormat" 
         #maxDateControl="ngModel"></md2-datepicker> 
    ', 
}) 

export class CalendarComponent implements OnInit { 
    //declare this EventListener in order to listen on it in the parent component. 
    @Output() onSelectValue = new EventEmitter<{minDate: Date , maxDate: Date>(); 
    today: Date = new Date(); 


    minDate: Date; 
    maxDate: Date; 

    constructor(){ 

    } 

    ngOnInit(){ 

    } 

    onChange() { 
    this.onSelectValue.emit({minDate: this.minDate, maxDate:this.maxDate}); 
    } 

} 
0

実際には、このcookbookのangular.ioウェブサイトに詳細が記載されています。 Component Interaction

私の個人的なお気に入りは、親子通信のためのサービスを使用することですが、もちろんそれは状況によって異なります。

0

別の解決策:

チャイルドコンポーネント:

import { 
 
    Component, 
 
    OnInit, 
 
    ViewChild, 
 
    Input 
 
} from '@angular/core'; 
 

 
@Component({ 
 
    selector: 'calendar', 
 
    template: ` 
 
    <md2-datepicker name="mindate" 
 
         placeholder="Minimum Date" 
 
         [(ngModel)]="minDate" 
 
         [mode]="mode" 
 
         [touchUi]="touch" 
 
         [format]="dateFormat" 
 
         #minDateControl="ngModel"></md2-datepicker> 
 
     <md2-datepicker name="maxdate" 
 
         placeholder="Maximum Date" 
 
         [(ngModel)]="maxDate" 
 
         [min]="minDate" 
 
         [mode]="mode" 
 
         [touchUi]="touch" 
 
         [format]="dateFormat" 
 
         #maxDateControl="ngModel"></md2-datepicker> 
 
    `, 
 
}) 
 

 
export class CalendarComponent implements OnInit { 
 

 
    minDate: Date; 
 
    maxDate: Date; 
 

 
    constructor(){ 
 

 
    } 
 

 
    ngOnInit(){ 
 

 
    } 
 

 
}

親コンポーネント:

import { 
 
    Component, 
 
    OnInit, 
 
    ViewChild, 
 
    Input, 
 
    ElementRef 
 
} from '@angular/core'; 
 

 
import { Router } from '@angular/router'; 
 

 
import { ChartModule,ChartComponent } from 'angular2-chartjs'; 
 
import { ChartService } from './../shared/chart.service'; 
 

 
import { Colors, xAxisWidth } from './../shared/data'; 
 

 
@Component({ 
 
    selector: 'form-js', 
 
    template: ` 
 
    <h3 class="chartHeading">Parameter Selection Form</h3> 
 
    <calendar #test></calendar> 
 
    <button (click)="showAlert();"></button> 
 
    `, 
 
}) 
 

 
export class FormComponent implements OnInit { 
 

 
    @ViewChild('test') calendar; 
 

 
    constructor(){ 
 

 
    } 
 

 
    ngOnInit(){ 
 

 
    } 
 

 
    showAlert(){ 
 
    alert(this.calendar.minDate); 
 
    } 
 

 
}

showAlert()メソッドでngModelプロパティにアクセスできます。

関連する問題