Reputation: 367
I have looked at SEVERAL other posts about this and I'm still having a problem implementing ngmodel binding with matdatepicker.
My HTML:
<mat-form-field>
<mat-label>Start Date</mat-label>
<input matInput [(ngModel)]="searchStartDate" [min]="minDate" [max]="maxDate" [matDatepicker]="picker">
<mat-datepicker-toggle matSuffix [for]="picker">
</mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
I get this error for the html code above: Can't bind to 'ngModel' since it isn't a known property of 'input'.
TS file associated with html code above:
import { Component, OnInit } from '@angular/core';
import { FormsModule,ReactiveFormsModule } from '@angular/forms';
// I threw this in just in case^^
@Component({
selector: 'app-dash-home',
templateUrl: './example.component.html',
styleUrls: ['./example.component.scss']
})
export class ExampleComponent implements OnInit {
searchStartDate: Date; searchEndDate: Date
constructor() {
const todaysDate = new Date();
this.minDate = new Date(todaysDate.getFullYear() - 5, 0, 1);
this.maxDate = new Date(todaysDate.getFullYear(), todaysDate.getMonth(), todaysDate.getDate());
}
ngOnInit() {
}
}
I have been trying to debug this issue for several hours now and I'm not sure if this is an Angular v9 issue. I found a stackblitz example from another stackoverflow question which illustrates the expected behavior here (running Angular v7 I think): https://stackblitz.com/edit/angular-y8hqyn-52t76k?file=app%2Fdatepicker-value-example.html
Notice how the stackblitz example initializes to today's date? That's the behavior I'm trying to emulate. I also included my app.module.ts below just in case.
File: app.module.ts
import { FormsModule, ReactiveFormsModule} from '@angular/forms';
imports: [...
FormsModule,
...]
Thanks for all the anticipated help and support
EDIT:
Adding the module files for which ExampleComponent belongs to:
examplecomponent.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MaterialModule } from '../../assets/material';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [...],
imports: [
...,
FormsModule
]
})
export class ExampleComponentModule{ }
Upvotes: 0
Views: 838
Reputation: 1956
You need to make sure that the FormsModule
is imported in the module that your ExampleComponent
is declared
Upvotes: 2