[英]NgbDatepicker shared component popup toggle() is not a function
I have a ngbdatePicker shared component for popup and trying to use it in one of the components, but open/close (toggle) is not working.我有一个用于弹出窗口的 ngbdatePicker 共享组件,并尝试在其中一个组件中使用它,但打开/关闭(切换)不起作用。
I tried using NgbDatepicker and NgbInputDatepicker, but same issue.我尝试使用 NgbDatepicker 和 NgbInputDatepicker,但同样的问题。 I'm trying to open/close the popup when pressing a button.
我试图在按下按钮时打开/关闭弹出窗口。 Also if I use the "Range selection in a popup" example directly from ng-bootstrap, it works as expected.
此外,如果我直接从 ng-bootstrap 使用“弹出窗口中的范围选择”示例,它会按预期工作。 But the problem is from shared component (parent) to other components.
但问题是从共享组件(父)到其他组件。
The error is:错误是:
ERROR TypeError: this.dateInputPicker.toggle is not a function
at DatePickerComponent.toggle (date-picker.component.ts:58:24)
Code snippet:代码片段:
Shared Component HTML:共享组件 HTML:
<div class="icon-small" *ngIf="!simpleDatePicker">
<form class="row row-cols-sm-auto">
<div class="col-12">
<div class="dp-hidden position-absolute">
<div class="input-group">
<input
id="dp1"
name="datepicker"
class="form-control"
ngbDatepicker
#dp1
[autoClose]="'outside'"
(dateSelect)="onDateSelection($event)"
[displayMonths]="2"
outsideDays="hidden"
[startDate]="fromDate!"
/>
</div>
</div>
</div>
</form>
</div>
Shared component ts:共享组件 ts:
@Component({
selector: 'app-date-picker',
templateUrl: './date-picker.component.html',
styleUrls: ['./date-picker.component.css']
})
export class DatePickerComponent implements OnInit {
...
@ViewChild('dp1') dateInputPicker: NgbInputDatepicker;
...
...
toggle(event) {
console.log(event.dp1); //logs as undefined
this.dateInputPicker.toggle(); //Errors as this.dateInputPicker.toggle is not a function
}
Popup component HTML:弹出组件 HTML:
<form class="row row-cols-sm-auto">
<div class="col-12">
<div class="dp-hidden position-absolute">
<div class="input-group">
<app-date-picker
name="datepicker"
class="form-control"
ngbDatepicker
#dp1
[simpleDatePicker]="false"
[autoClose]="'outside'"
(dateSelect)="onDateSelection($event)"
[displayMonths]="2"
[dayTemplate]="t"
outsideDays="hidden"
[weekdays]="false"
[startDate]="fromDate!"
tabindex="-1"></app-date-picker>
<ng-template #t let-date let-focused="focused">
<span
class="custom-day"
[class.focused]="focused"
[class.range]="isRange(date)"
[class.faded]="isHovered(date) || isInside(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null"
>
{{ date.day }}
</span>
</ng-template>
</div>
</div>
<div class="input-group">
<input
#dpFromDate
class="form-control"
placeholder="yyyy-mm-dd"
name="dpFromDate"
[value]="formatter.format(fromDate)"
(input)="fromDate = validateInput(fromDate, dpFromDate.value)"
/>
<button class="btn btn-outline-secondary bi bi-calendar3 (click)="dp1.toggle($event)" type="button"></button>
</div>
</div>
this.dateInputPicker is an instance of ElementRef assigning the type as NgbInputDatepicker will not work. this.dateInputPicker 是 ElementRef 的实例,分配类型为 NgbInputDatepicker 将不起作用。
Shared Component HTML:共享组件 HTML:
<input
id="dp1"
name="datepicker"
class="form-control"
ngbDatepicker
#dp1="ngbDatepicker"
[autoClose]="'outside'"
(dateSelect)="onDateSelection($event)"
[displayMonths]="2"
outsideDays="hidden"
[startDate]="fromDate!"
/>
Shared component ts:共享组件 ts:
@ViewChild('dp1') dp1: NgbInputDatepicker;
toggle() {
this.dp1.toggle();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.