Ionic 使用离子日期时间的自定义Angular组件如果未被触及,将无法正常工作

ozxc1zmp  于 11个月前  发布在  Ionic
关注(0)|答案(1)|浏览(158)

我构建了一个自定义的Angular 日期选择器组件,它具有双向绑定(它从firestore获取值,如果用户修改它,它会更新到firestore)。
这是我的HTML

<ion-item>
  <ion-list>
    <ion-item id="{{'open-date-' + pickerId}}">
      <ion-label>{{label}}</ion-label>
      <ion-note slot="end">{{selectedDate | date: 'HH:mm'}}</ion-note>
    </ion-item>
    <ion-modal trigger="{{'open-date-' + pickerId}}" [cssClass]="'bottom-end'">
      <ng-template>
        <ion-datetime
          presentation="time"
          [(ngModel)]="selectedDate"
          (ionChange)="onDateChange(selectedDate)"
          size="cover"
          [showDefaultButtons]="true"
          cancelText="Annuler"
          doneText="Valider"
          minuteValues="0,15,30,45">
        </ion-datetime>
      </ng-template>
    </ion-modal>
  </ion-list>
</ion-item>

字符串
这是我的TS

import { Component, OnInit, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-datetime-picker',
  templateUrl: './datetime-picker.component.html',
  styleUrls: ['./datetime-picker.component.scss'],
})
export class DatetimePickerComponent implements OnInit {
  @Input() pickerId?: string;
  @Input() label?: string;
  @Input() selectedDate: Date = new Date();
  @Output() selectedDateChange = new EventEmitter<Date>();

  constructor() { }

  ngOnInit() {}

  onDateChange(newDate: Date): void {
    console.log('onDateChange', newDate);
    this.selectedDate = newDate;
    this.selectedDateChange.emit(this.selectedDate);
  }
}


不幸的是,只有当我点击日期,使用“滚动”时间选择器,然后验证时,它才能工作。如果我只是打开,并点击验证,我应该用当前时间更新firestore值(打开时间选择器时的默认值),这是一个未定义的日期。
我尝试使用setter和getter,我尝试使用ngModelChange,以及更多的解决方案,但问题仍然是完全相同的。

pwuypxnk

pwuypxnk1#

如何使用ionFocus事件并设置当前时间和发出事件
HTML

<ion-item>
  <ion-list>
    <ion-item id="{{'open-date-' + pickerId}}">
      <ion-label>{{label}}</ion-label>
      <ion-note slot="end">{{selectedDate | date: 'HH:mm'}}</ion-note>
    </ion-item>
    <ion-modal trigger="{{'open-date-' + pickerId}}" [cssClass]="'bottom-end'">
      <ng-template>
        <ion-datetime
          presentation="time"
          [(ngModel)]="selectedDate"
          (ionChange)="onDateChange(selectedDate)"
          (ionFocus)="onDatePickerFocus()"
          size="cover"
          [showDefaultButtons]="true"
          cancelText="Annuler"
          doneText="Valider"
          minuteValues="0,15,30,45">
        </ion-datetime>
      </ng-template>
    </ion-modal>
  </ion-list>
</ion-item>

字符串
ts

import { Component, OnInit, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-datetime-picker',
  templateUrl: './datetime-picker.component.html',
  styleUrls: ['./datetime-picker.component.scss'],
})
export class DatetimePickerComponent implements OnInit {
  @Input() pickerId?: string;
  @Input() label?: string;
  @Input() selectedDate: Date = new Date();
  @Output() selectedDateChange = new EventEmitter<Date>();

  constructor() { }

  ngOnInit() {}

  onDateChange(newDate: Date): void {
    console.log('onDateChange', newDate);
    this.selectedDate = newDate;
    this.selectedDateChange.emit(this.selectedDate);
  }

  onDatePickerFocus(): void {
    console.log('onDatePickerFocus', newDate);
    if(!this.selectedDate) {
        this.selectedDate = new Date();
    }
    this.selectedDateChange.emit(this.selectedDate);
  }
}

相关问题