下面是Angular Material日期选择器和输入框的解决方法,包含代码示例:
npm install --save @angular/material @angular/cdk @angular/animations
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatInputModule } from '@angular/material/input';
import { MatNativeDateModule } from '@angular/material/core';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatDatepickerModule,
MatInputModule,
MatNativeDateModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
selectedDate: Date;
constructor() {
this.selectedDate = new Date();
}
}
这样,你就可以在Angular应用中使用Angular Material的日期选择器和输入框了。当你选择一个日期时,它将被绑定到selectedDate变量中。你可以根据需要修改和扩展这些示例代码。