I have a component named search_detail which has another component inside it named calendar,
SearchDetail_component.html
JavaScript
x
14
14
1
<li class="date">
2
<div class="btn-group dropdown" [class.open]="DatedropdownOpened">
3
<button type="button" (click)="DatedropdownOpened = !DatedropdownOpened" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
4
aria-haspopup="true" [attr.aria-expanded]="dropdownOpened ? 'true': 'false'">
5
Date <span class="caret"></span>
6
</button>
7
<ul class="dropdown-menu default-dropdown">
8
<calendar ></calendar>
9
<button > Cancel </button>
10
<button (click)="setDate(category)"> Ok </button>
11
</ul>
12
</div>
13
</li>
14
SearchDetail_component.ts
JavaScript
1
8
1
import 'rxjs/add/observable/fromEvent';
2
@Component({
3
selector: 'searchDetail',
4
templateUrl: './search_detail.component.html',
5
moduleId: module.id
6
7
})
8
Calendar.component.ts
JavaScript
1
26
26
1
import { Component, Input} from '@angular/core';
2
@Component({
3
moduleId:module.id,
4
selector: 'calendar',
5
templateUrl: './calendar.component.html'
6
})
7
8
export class CalendarComponent{
9
public fromDate:Date = new Date();
10
private toDate:Date = new Date();
11
private events:Array<any>;
12
private tomorrow:Date;
13
private afterTomorrow:Date;
14
private formats:Array<string> = ['DD-MM-YYYY', 'YYYY/MM/DD', 'DD.MM.YYYY', 'shortDate'];
15
private format = this.formats[0];
16
private dateOptions:any = {
17
formatYear: 'YY',
18
startingDay: 1
19
};
20
private opened:boolean = false;
21
22
public getDate():number {
23
return this.fromDate.getDate() || new Date().getTime();
24
}
25
}
26
I want to access the startdate and enddate on click of the ok button in the search detail page. how can i do?
Advertisement
Answer
Register the EventEmitter
in your child component as the @Output
:
JavaScript
1
2
1
@Output() onDatePicked = new EventEmitter<any>();
2
Emit value on click:
JavaScript
1
4
1
public pickDate(date: any): void {
2
this.onDatePicked.emit(date);
3
}
4
Listen for the events in your parent component’s template:
JavaScript
1
4
1
<div>
2
<calendar (onDatePicked)="doSomething($event)"></calendar>
3
</div>
4
and in the parent component:
JavaScript
1
4
1
public doSomething(date: any):void {
2
console.log('Picked date: ', date);
3
}
4
It’s also well explained in the official docs: Component interaction.