要实现一个24小时时间选择器,可以使用Angular Material中的MatTimepicker
组件。以下是一个基本示例:
首先,确保你已经安装了必要的依赖:
npm install @angular/material
npm install @angular/cdk
在你的模块中导入MatTimepickerModule
:
import { NgModule } from '@angular/core';
import { MatTimepickerModule } from '@angular/material/timepicker';
@NgModule({
imports: [
MatTimepickerModule
]
})
export class YourModule { }
在你的组件中使用MatTimepicker
:
schedule
最后,你需要在你的组件类中添加一些代码来处理时间选择的结果:
import { Component, ViewChild } from '@angular/core';
import { NgxMaterialTimepickerComponent } from 'ngx-material-timepicker';
@Component({
selector: 'your-component',
templateUrl: 'your.component.html',
styleUrls: ['your.component.css']
})
export class YourComponent {
@ViewChild('picker') picker: NgxMaterialTimepickerComponent;
selectedTime: string;
openTimePicker() {
this.picker.open();
}
handleTimeSelection(event) {
this.selectedTime = event;
}
}
这样就可以实现一个基本的24小时时间选择器。当用户选择时间时,handleTimeSelection
方法将被调用,并将选定的时间存储在selectedTime
变量中。
你可以根据需要自定义时间选择器的外观和行为。有关更多详细信息,请参阅Angular Material和ngx-material-timepicker的文档。