我正在尝试使用 bootstrap ngbdatepicker 我将 datepicker 选择器添加到 appcomponent.html 并且 datepicker 开始在那里显示 现在我需要将该模型值读入控制器,以便我可以将它传递给父 appcomponent 我在 datepicker 上添加(更改)方法但它是从输入中删除该方法,以便我如何读取该值并将其传递给父级。是否有任何其他选项可以在控制器中读取该值并传递给父级
提前致谢
下面是我在这里使用子选择器打开日期选择器的模板我添加了更改方法,但它没有触发日期选择,所以我无法发出事件
父组件:
<ng-template #modalContent let-close="close" *ngIf="true">
<div class="modal-header">
<h5 class="modal-title">Add new event</h5>
<button type="button" class="close" (click)="close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<label>Start Date</label><date-pick (change)="updateFromChild($event)"></date-pick>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" (click)="close()">OK</button>
</div>
</ng-template>
Child Component:
import {Component, Input, Output, EventEmitter} from '@angular/core'
@Component({
selector: 'date-pick',
templateUrl: './datepicker-popup.html'
})
export class NgbdDatepickerPopup {
model;
constructor(){ }
@Output() change: EventEmitter<any> = new EventEmitter<any>();
onChange() {
console.log('call');
this.change.emit(this.model)
}
}
Child Template:
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input class="form-control" placeholder="yyyy-mm-dd"
name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker" (change)="onChange()">
<div class="input-group-append">
<button class="btn btn-outline-secondary" (click)="d.toggle()" type="button">
<img src="img/calendar-icon.svg" style="width: 1.2rem; height: 1rem; cursor: pointer;"/>
</button>
</div>
</div>
</div>
</form>