Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material DatePicker - Error: MdDatepicker: No provider found for DateAdapter

Following Material docs, I:

import {MdDatepickerModule} from '@angular/material';

in root module and added it in providers list, but coudln't use it in template. Got this error:

Uncaught (in promise): Error: MdDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MdNativeDateModule, or provide a custom implementation.

So according to it, I removed last import and added:

import {MdNativeDateModule} from '@angular/material';

And now, looks like, it works. Why Material docs tells to import MdDatepickerModule, but not MdNativeDateModule? And how they differ?

like image 678
Julius Dzidzevičius Avatar asked Sep 21 '17 07:09

Julius Dzidzevičius


2 Answers

The example in the docs actually does import MdNativeDateModule. Click on the plunker link and see the main.ts file. The docs are not as clear about this as would be preferred.

MdDatepickerModule is the module containing the component itself. The component is agnostic to which implementation of dates you use with it.

For example, if you wished to use Moment.JS dates instead of JavaScript's native Date objects, you could write your own class to work with Moment.JS objects. This is explained here.

If you simply want to work with JavaScript's Date objects, though, you would need to use the NativeDateAdapter class that is already included in @angular/material. This class is provided by the MdNativeDateModule.

It is not provided by MdDatepickerModule since that module deals only with the UI component, and in case you were using Moment.JS objects, you would have no need for the NativeDateAdapter class in your application.

like image 82
Aakash Jain Avatar answered Sep 27 '22 22:09

Aakash Jain


you need to import MatNativeDateModule from @angular/material once you imported, you should have to import and export it in @NgModule you can see on angular material

like image 25
Hayatullah Rahnamoon Avatar answered Sep 27 '22 21:09

Hayatullah Rahnamoon