Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material 17 No provider for DateAdapter in Standalone Component

I have a nx project with angular 17 app and try to use a the angular material date picker with material 17.0.4. But I get the

NullInjectorError: No provider for DateAdapter!

Error. The components are standalone components and so there is no app.module file.

The component

@Component({
  selector: 'my-component',
  standalone: true,
  imports: [
    MatFormFieldModule,
    MatInputModule,
    MatDatepickerModule,
    FormsModule,
    MatIconModule,
    MatButtonModule,
    MatNativeDateModule,
    NgIf
  ],
  providers: [MatNativeDateModule],
  templateUrl: './edit-booking.component.html',
  styleUrl: './edit-booking.component.css',
})
export class EditBookingComponent implements OnInit {}

I also tried to import the providers in the app.config.ts

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(appRoutes),
    provideAnimations(),
    provideHttpClient(withInterceptors([authInterceptor])),
    {provide: LOCALE_ID, useValue: 'de-AT'},
    importProvidersFrom(MatNativeDateModule)

  ],
};

but then I get this error:

Cannot read properties of undefined (reading 'liveCollection')

Thanks a lot!

like image 516
miholzi Avatar asked Oct 31 '25 14:10

miholzi


1 Answers

for anyone having this same problem, and the above solution doesn't work, add this to your dialog component:

providers: [ 
   {provide: DateAdapter, useClass: NativeDateAdapter},
   {provide: MAT_DATE_FORMATS, useValue: MAT_NATIVE_DATE_FORMATS}
]

this worked for me,

source: See last comment

like image 85
Cristhian Casierra Avatar answered Nov 03 '25 07:11

Cristhian Casierra