In my template-driven form, I am encountering the following error:
ERROR TypeError: Cannot read property 'invalid' of undefined
I am unsure why this error is happening. How can I resolve this issue?
Here is my app.component.html code:
<form name="form" (ngSubmit)="f.form.valid && onSubmit()" #f="ngForm" novalidate>
<div class="form-group">
<label for="categoryName">Category Name:</label>
<input type="text" class="form-control" name="categoryName" [(ngModel)]="cate.categoryName" minlength="5" #categoryname="ngModel" [ngClass]="{ 'is-invalid': f.submitted && categoryName.invalid }" required/>
<div *ngIf="f.submitted && categoryName.invalid" class="invalid-feedback">
<div *ngIf="categoryName.errors.required">
Category Name is required
</div>
</div>
<div *ngIf="categoryName?.touched && categoryName?.errors.minlength" class="invalid-feedback">
Category Name must be at least 5 characters long.
</div>
</div>
</form>
And here is my app.component.ts code:
cate: any = {};
onSubmit() {
alert(this.cate.categoryName);
}