I am sure this is simple. I have this html:
<form [formGroup]="formGroup">
<mat-form-field class="form-group" appearance="outline">
<mat-label>Choose your start date</mat-label>
<input matInput formControlName="startDate" aria-label="startDate"
[matDatepicker]="start">
<mat-datepicker-toggle matSuffix [for]="start"></mat-datepicker-toggle>
<mat-datepicker #start></mat-datepicker>
</mat-form-field>
<mat-form-field class="form-group" appearance="outline">
<mat-label>End date</mat-label>
<input matInput formControlName="endDate" aria-label="endDate" [matDatepicker]="end">
<mat-datepicker-toggle matSuffix [for]="end"></mat-datepicker-toggle>
<mat-datepicker #end></mat-datepicker>
</mat-form-field>
<div class="form-group">
<mat-slide-toggle formControlName="rolling">Continue until further notice?
</mat-slide-toggle>
</div>
</form>
I am trying to only enable the endDate if the rolling is toggled off.
I have tried doing it like this:
public formGroup: FormGroup;
public subscription: Subscription;
// convenience getter for easy access to form fields
get f() {
return this.formGroup.controls;
}
constructor(
private formBuilder: FormBuilder
) {}
ngOnInit(): void {
this.createForm();
}
private createForm(): void {
this.formGroup = this.formBuilder.group({
startDate: new FormControl(
this.subscription.startDate,
Validators.required
),
rolling: this.subscription.rolling,
});
this.formGroup.addControl(
'endDate',
new FormControl({
value: this.subscription.endDate,
disabled: this.f.rolling.value === true,
})
);
}
But when I toggle the slider, nothing happens. The input is always disabled. Does anyone know why?