在我的EventCreate的TypeScript类中,我有带有数据类型Date的startDateTime和endDateTime属性。HTML5使用输入类型time来获取时间。我只想问一问:如何使输入类型time与TypeScript和Angular2一起工作?
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { EventCreate } from './eventCreate';
@Component({
selector: 'add-Event',
templateUrl: './event-add.component.html',
})
export class EventAddComponent {
title: string;
description: string;
locationId: number;
locationDetails: string;
categoryId: number;
isRegistrationRequired: boolean;
eventDate: Date;
startDateTime: Date;
endDateTime: Date;
maximumRegistrants: number;
newEvent: EventCreate;
constructor(private router: Router) { }
createNewEvent() {
console.log('new Event Created');
this.newEvent = new EventCreate(
this.title,
this.description,
this.locationId,
this.locationDetails,
this.categoryId,
this.isRegistrationRequired,
this.eventDate,
this.startDateTime,
this.endDateTime,
this.maximumRegistrants
);
console.log(this.newEvent);
//call service
//call route to go to Home page
this.router.navigate(['/home']);
}
cancel() {
this.router.navigate(['/home']);
}
}
export class EventCreate {
constructor(
public title: string,
public description: string,
public locationId: number,
public locationDetails: string,
public categoryId: number,
public isRegistrationRequired: boolean,
public eventDate: Date,
public startDateTime: Date,
public endDateTime: Date,
public maximumRegistrants: number,
) { }
}
<form>
<div class="form-group">
<label>Start Time:</label>
<input type="time" name="startDateTime" [(ngModel)]="startDateTime">
</div>
<div class="form-group">
<label>End Time:</label>
<input type="time" name="endDateTime" [(ngModel)]="endDateTime">
</div>
</form>发布于 2019-09-19 20:17:23
我知道现在有点晚了,但不管怎样都能帮上忙。输入类型" time“输出输入时间的字符串表示形式。如果您的目标是从这个"time“字符串中获取一个类型记录日期对象(出于某些原因),我建议您查看输入类型日期时间-本地,然后将其值转换为类型记录日期对象为new Date(your_input_value)。下面是一个示例代码(我使用的是反应形式和角度材料):
<form [formGroup]="meetingForm" novalidate autocomplete="off">
.............. other input fields .....................
<input matInput
type="datetime-local"
placeholder="Start Time"
formControlName="meetingStartTime">
....................................
</form>然后组件类的一部分看起来类似于
.........other codes................
constructor(formBuilder:FormBuilder, ...){}
...............................
let meetingStartTimeFormControl = this.meetingForm.get('meetingStartTime') as FormControl;
this.startDateTime = new Date(this.meetingStartTimeFormControl.value);
console.log(this.startDateTime)https://stackoverflow.com/questions/43742531
复制相似问题