Angular Material Date-Picker is not working properly

Viewed 25013

I am trying to get date using Angular Material datepicker as mentioned bellow,

In html

<mat-form-field>
<mat-label>Custom calendar color</mat-label>
<input matInput [matDatepicker]="picker2">
<mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
<mat-datepicker #picker2 color="primary"></mat-datepicker>
</mat-form-field>

In app.module.ts

import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material';

imports: [
  MatDatepickerModule,
  MatNativeDateModule
],

In my package.json

"dependencies": { "@angular/animations": "^8.2.9", "@angular/cdk": "^8.2.3",
"@angular/core": "~8.2.0",
"@angular/material": "^8.2.3",
"@angular/router": "~8.2.0", "angular-mat-datepicker": "0.0.2", "angular-material-datepicker": "^1.0.2",
"hammerjs": "^2.0.8", "ng-material-datetimepicker": "^1.19.2",
},

But the date picker icon is not working, even icon is not showing in the view. I have updated Angular material version as well but that didn't work. What is the solution for this problem?

7 Answers

You also have to import MatFormFieldModule and MatInputModule to get it working.

Also don't forget to import BrowserAnimationsModule. Check out this StackBlitz

This is the solution for my problem.I just run bellow code and now everything works fine as I expected,

ng add @angular/material

Also import this link:

import { MatNativeDateModule, MatRippleModule } from '@angular/material/core';

You might be missing the startView and/or [startAt] attribute, this is how the Angular docs suggest to build up a simple datepicker in your html:

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker startView="year" [startAt]="startDate"></mat-datepicker>
</mat-form-field>

I have used material datepicker in angular 7. Go through this and hope it will resolve your issue.

import {Component, OnInit, ViewChild} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {MatDatepicker} from '@angular/material';

@Component({
selector: 'app-date',
template: `

    <mat-form-field class="demo-full-width margin-top" [formGroup]="group">
        <input matInput [matDatepicker]="picker" (dateChange)="saveRange()"
               formControlName="datePicker" placeholder="Select Date">
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
        <mat-datepicker #picker></mat-datepicker>
    </mat-form-field>

`,
styles: [],
})

export class DateComponent implements OnInit {

group: FormGroup;

constructor() {
}

ngOnInit() {
}

saveRange() {
    console.log(this.group.value);
}

}

import {MatIconModule} from '@angular/material/icon';

try this

In my case it was (weirdly) that the z-index of the datepicker's overlay was too high, meaning that any click on the datepicker was actually just clicking on the overlay. Lowering the z-index of it fixed the problem:

.cdk-overlay-backdrop {
    z-index: 100; //instead of z-index: 1031;
}
Related