1

I'm trying to add an Angular Material datepicker to the page. I've been following the official doc's example like so, but nothing displays in my page.

In the app.component.html there is:

<section>
  <mat-form-field appearance="fill">
    <mat-label>Choose a date</mat-label>
    <input matInput [matDatepicker]="picker" />
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>
</section>

And in the app.component.ts, I have:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  panelOpenState = false;
}

And this is my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MatDatepickerModule,
    MatFormFieldModule,
    MatInputModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Can someone please tell me what I'm doing wrong?

Newsha Nik
  • 806
  • 2
  • 12
  • 29

1 Answers1

0

I think you have not imported the FormsModule in the @NgModule.

Please check the below example and its main.ts file. It's working.

Demo Example

I hope it will help.

Amit Vishwakarma
  • 316
  • 2
  • 10