, , . , .
- autofocus
. Angular " , ." . FormControl. name.untouched
false
true
.
you can observe this if you add extra debugging bindings to your template. eg.
<hello name="{{ name }}"></hello>
<form [formGroup]="form">
<input formControlName="name" autofocus>
<button (click)="onClick($event)">click OK</button>
</form>
<pre>{{form.controls.name.valid}}</pre>
<pre>{{form.controls.name.untouched}}</pre>
<p [hidden]="form.controls.name.valid || form.controls.name.untouched ">
Invalid :)
</p>
<button (click)="onClick2($event)">click NOT ok</button>
If you want to hide the error message when the user presses the bottom button, you must remove autofocus
or reset the form check when called onClick2()
. Alternatively, you can simply display an error message when the form has an error. Angular says A control is untouched if the user has not yet triggered a blur event on it.
When the bottom button is pressed, a blur event occurs.
Why not just change the template binding to the error message
<p [hidden]="!form.controls.name.valid || form.controls.name.untouched">
Invalid :)
</p>
source
share