简体   繁体   English

NgbDatepicker 共享组件 popup toggle() 不是函数

[英]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 将不起作用。

  • Add ngbDatepicker directive to the input element (#dp1)将 ngbDatepicker 指令添加到输入元素 (#dp1)
  • Assign the template ref as NgbInputDatepicker将模板引用分配为 NgbInputDatepicker
  • When the toggle method is called, use the dp1 template ref to toggle the popup当调用 toggle 方法时,使用 dp1 模板 ref 来切换弹出窗口

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.

相关问题 使用打字稿中的 Angular 关闭 ngbDatePicker 弹出窗口 - Close a ngbDatePicker popup with Angular from the typescript 错误:“formControlName 必须与父 formGroup 指令一起使用。” 为了<input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()"> - Error: "formControlName must be used with a parent formGroup directive." for <input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()"> 具有特定点击功能的共享组件 - Shared component with specific click function 除了在 ngAfterViewInit() 上使用它之外,有没有办法在组件上使用 NgbDatepicker 的 navigateTo()? - Is there a way to use NgbDatepicker's navigateTo() on the component other than using it on ngAfterViewInit()? 在自定义组件内的NgbDatePicker,如何传播验证器和样式? - NgbDatePicker inside a custom component, how to propagate validators and styles? NgbDatepicker markDisabled没有其父组件的范围 - NgbDatepicker markDisabled does not have scope of its parent component 从共享服务中调用组件的功能 - Call function of a component from within a shared service 如何在主要组件中设置 NgbDatePicker 的 minDate/maxDate 以使用所有页面中的设置? - How to set minDate/maxDate of NgbDatePicker in main component for using the settings in all page? 在Angular 8组件中切换MatSlideToggle - toggle MatSlideToggle in Angular 8 component 角度组件进入传单弹出窗口 - angular component into leaflet popup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM