从子组件向父组件传递数据 Angular2
我有一个名为 search_detail 的组件,其内部有另一个名为日历的组件、
SearchDetail_component.html
<li class="date">
<div class="btn-group dropdown" [class.open]="DatedropdownOpened">
<button type="button" (click)="DatedropdownOpened = !DatedropdownOpened" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" [attr.aria-expanded]="dropdownOpened ? 'true': 'false'">
Date <span class="caret"></span>
</button>
<ul class="dropdown-menu default-dropdown">
<calendar ></calendar>
<button > Cancel </button>
<button (click)="setDate(category)"> Ok </button>
</ul>
</div>
</li>
SearchDetail_component.ts
import 'rxjs/add/observable/fromEvent';
@Component({
selector: 'searchDetail',
templateUrl: './search_detail.component.html',
moduleId: module.id
})
Calendar.component.ts
import { Component, Input} from '@angular/core';
@Component({
moduleId:module.id,
selector: 'calendar',
templateUrl: './calendar.component.html'
})
export class CalendarComponent{
public fromDate:Date = new Date();
private toDate:Date = new Date();
private events:Array<any>;
private tomorrow:Date;
private afterTomorrow:Date;
private formats:Array<string> = ['DD-MM-YYYY', 'YYYY/MM/DD', 'DD.MM.YYYY', 'shortDate'];
private format = this.formats[0];
private dateOptions:any = {
formatYear: 'YY',
startingDay: 1
};
private opened:boolean = false;
public getDate():number {
return this.fromDate.getDate() || new Date().getTime();
}
}
我想在点击搜索详情页中的确定按钮时访问开始日期和结束日期,该怎么做?
39
2
在子组件中将
EventEmitter
注册为@Output
:点击时发射值:
监听父组件模板中的事件:
和父组件中的事件:
官方文档中也有详细解释:组件交互.
您好,您可以使用输入和输出。 输入可以将变量从父代传递给子代。输出也是一样,但是从子代到父代。
最简单的方法是将 "startdate" 和 "endDate" 作为输入进行传递
这样,您就可以在搜索页面直接找到开始日期和结束日期。 请告诉我是否可行,或者是否有其他办法。 谢谢