My form is connected to a model as shown below
In the component file:
myTextModel: string;
updateMyTextModel(): void {
this.myTextModel = "updated model value";
//todo- set form dirty (or invalid or touched) here
}
Html template:
<form #testForm="ngForm" id="testForm">
<input type="text" id="myText" [(ngModel)]="myTextModel" name="myText" #myText="ngModel">
</form>
<button (click)="updateMyTextModel()">Update myTextModel</button>
<div *ngIf="testForm.dirty">testForm dirty</div>
<div *ngIf="testForm.touched">testForm touched</div>
I am looking for a way to programmatically mark the form as touched or dirty. How can I achieve this?
Please note: Although in this example a button is used to update the model, it could also be updated through other means like a callback from an asynchronous web API request.