要使用Angular Material日期选择器,您需要按照以下步骤进行设置:
ng add @angular/material
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatInputModule } from '@angular/material/input';
import { MatNativeDateModule } from '@angular/material/core';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule,
MatDatepickerModule,
MatInputModule,
MatNativeDateModule
],
bootstrap: [AppComponent]
})
export class AppModule {}
mat-datepicker
指令来创建日期选择器。例如,您可以在app.component.html文件中添加以下代码:
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
date = new FormControl();
}
这样,您就可以在组件中使用date.value
来获取所选的日期。
以上是使用Angular Material日期选择器的基本示例。您可以根据自己的需求进行进一步的定制和样式设置。