I had date picker which should dynamically disable and enable by checkbox toggle click. All components from library angular material 6. And cause I using a reactive approach for my form handlers I can't simply use [disable] directive. I got further error:
It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
you. We recommend using this approach to avoid 'changed after checked' errors.
Example:
form = new FormGroup({
first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
last: new FormControl('Drew', Validators.required)
});
And I have idea replace FormContol inside FormGroup directly in TS, something like this:
toggleDatePicker() {
this.isDateRange = !this.isDateRange;
this.form.removeControl('to');
if (this.isDateRange) {
this.form.addControl('to', new FormControl({value: new Date(), disabled: false}))
} else {
this.form.addControl('to', new FormControl({value: null, disabled: true}))
}
}
And this work for input tag but mat-datepicker-toggle remains with the initial state. mat-datepicker-toggle state not depending on FormControl.
<mat-form-field>
<input
matInput
[matDatepicker]="to"
formControlName="to"
[disabled]="isDateRange"
>
<mat-datepicker-toggle matSuffix [for]="to"></mat-datepicker-toggle> // THIS IS NO CHANGE(
<mat-datepicker #to></mat-datepicker>
</mat-form-field>
Independent from my FormControl manipulation mat-datepicker-toggle always in the same state:
How can I manipulate mat-datepicker-toggle thought FromControl?

