将Ionic 6日期时间选取器与ModalController配合使用

xkftehaa  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(191)

使用popover/modalController创建的弹出窗口/模态不会提前创建,因此也不会创建内部内容。
参考:https://ionicframework.com/docs/api/popover#mounting-inner-contents
因此,您不能在modalController呈现的组件中使用类似的内容:

// page component
async presentPopoverFilter(e: Event) {
  const popover = await this.modalController.create({
    component: PopoverFiltersComponent,
    componentProps: {
      profile: this.profile,
      filters: this.filters,
    },
  });

  await popover.present();
}

// modal component
<ion-datetime-button datetime="datetime"></ion-datetime-button>

<ion-popover [keepContentsMounted]="true">
  <ng-template>
    <ion-datetime id="datetime"></ion-datetime>
  </ng-template>
</ion-popover>

结果将是一个空弹出窗口。

wnavrhmk

wnavrhmk1#

New version:

I solve this using a PopoverController inside my modal component:

// template
<ion-item>
  <ion-label position="floating" color="primary">A partir</ion-label>
  <ion-input
    formControlName="timeStarts"
    (ionFocus)="presentTimeStartsPicker()"
  ></ion-input>
</ion-item>

// component
async presentTimeStartsPicker() {
  const popover = await this.popoverController.create({
    component: IonDatetime,
  });

  await popover.present();

  popover.firstChild.addEventListener('ionChange', (e: Event) => {
    this.filterForm.patchValue({ timeStarts: e.timeStamp });
  });
}

Old version:

I solve this using a PopoverController inside my modal component with a small component to render the IonDateTime.

// template
<ion-item>
  <ion-label position="floating" color="primary">A partir</ion-label>
  <ion-input
    formControlName="timeStarts"
    (ionFocus)="presentTimeStartsPicker()"
  ></ion-input>
</ion-item>

// component
async presentTimeStartsPicker() {
  const popover = await this.popoverController.create({
    component: TimePickerComponent,
    componentProps: {
      ionChange: (e: Event) => {
        this.filterForm.patchValue({ timeStarts: e.timeStamp });
      },
    },
  });
  await popover.present();
}

...

@Component({
  selector: 'app-popover-datepicker',
  template: `<ion-datetime (ionChange)="onChange($event)"></ion-datetime>`,
})
export class TimePickerComponent {
  @Input() ionChange: (e: Event) => void;

  onChange = (e: Event) => this.ionChange(e);
}

相关问题