私は子コンポーネント(カレンダー)と親コンポーネント(フォーム)を持っています。私はカレンダーで値の値を選択する必要があり、私はフォームコンポーネントの値にアクセスしたい。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のフォームコンポーネントの日付値?
共有サービスまたは@ Input/@ Outputプロパティのいずれかを使用できます。後者はこの場合に最も適しており、Angular docs:https:// angularに詳しく記述されています。io/guide/component-interaction –
提供されているサービスを利用して、データを渡すことができます。 – mast3rd3mon