我有一个名为search_detail的组件,其中包含另一个名为calendar的组件,
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();
}
}
我想点击搜索详细信息页面中的确定按钮来访问startdate和enddate . 我能怎么做?
2 回答
在子组件中注册
EventEmitter
作为@Output
:单击时发出值:
侦听父组件模板中的事件:
并在父组件中:
它在官方文档中也得到了很好的解释:Component interaction .
您好,您可以使用输入和输出 . 输入允许您将变量形式父级传递给子级 . 输出相同但从子到父 .
最简单的方法是将“startdate”和“endDate”作为输入
通过这种方式,您可以直接在搜索页面中使用startdate和enddate . 让我知道它是否有效,或者以另一种方式思考 . 谢谢