In my Angular application there is a template driven form.
<form #deviceForm="ngForm" (ngSubmit)="onSendClickHandler()">
<div >
<section>
<mat-checkbox
color="primary"
#confirmed1
>Product</mat-checkbox>
</section>
</div>
<div >
<mat-form-field appearance="outline">
<mat-label>Select Product</mat-label>
<mat-select
[disabled]="!confirmed1.checked" required>
<mat-option *ngFor="let product of productlist" [value]="product.code">
{{product.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<button
type="submit"
mat-button
[disabled]="!deviceForm.valid">
Send
</button>
</form>
When form is loading drop down list and send buttons are disabled. After click the checkbox it should be enable and when select any from list send button should be enable.
My problem is when I select product from list send button doesn't enable. Validation is working.
CodePudding user response:
Couple missing things in your approach:
- you need to add a
name
to your controls - you need to end these controls with an ngModel
I would highly recommend you use reactive forms instead of template driven, template driven is the older approach and it is clearly inferior to reactive forms.
However, here is a small working stackblitz of how to make your code work. I did rename your device form to myForm
, and I came up with a model for product list.
https://stackblitz.com/edit/angular-wro6wq?file=src/app/app.component.ts