Angular4 的一种形式的多个 ngb-timepicker

Multiple ngb-timepicker in one form with Angular4

我正在使用 Angular 4 创建一个带有开始时间和结束时间的表单,使用两个 ngb-timepicker。当我将模型设置为它们两个时,第一个实例化第二个的值。关于如何分别实例化它们的任何想法? 我的 HTML 看起来像这样:

<ngb-timepicker [(ngModel)]="time1"></ngb-timepicker>
<ngb-timepicker [(ngModel)]="time2"></ngb-timepicker>

我的组件:

import {Component} from '@angular/core';

@Component({
 selector: 'schedule-form',
 templateUrl: './schedule-form.component.html'
})

export class NgbdTimepickerBasic {
  time1 = {hour: startDateH, minute: startDateM};
  time2 = {hour: endDateH, minute: endDateM};
}

结果是time1会显示time2,所以只有time2是正确的。

您需要NgbTimeStruct格式化ngb-timepicker知道的日期

import {Component} from '@angular/core';
import {NgbTimeStruct} from '@ng-bootstrap/ng-bootstrap';

@Component({
 selector: 'schedule-form',
 templateUrl: './schedule-form.component.html'
})

export class NgbdTimepickerBasic {
  //declaration
  time1: NgbTimeStruct;
  time2: NgbTimeStruct;

  ngOnInit() {
    //get your start and end time and assign it to below 
    this.time1 = {hour: 13, minute: 30, second: 0};
    this.time2 = {hour: 16, minute: 15, second: 0};
  }

}