The above answer did not work with Angular 6 . So that's how I decided it. Let's say that I defined my input field -
<input type="number" id="myTextBox" name="myTextBox" [(ngModel)]="response.myTextBox" #myTextBox="ngModel">
To check if a field is empty or not, it must be a script.
<div *ngIf="!myTextBox.value" style="color:red;"> Your field is empty </div>
Note the subtle difference between the answer above and this answer. I added an extra .value attribute after my input name myTextBox . I donโt know if the above answer worked for the above version of Angular, but for Angular 6 it should be done.
A few more explanations of why this check works; if there is no value in the input field, the default value of myTextBox.value will be undefined . As soon as you enter some text, your text will become the new value myTextBox.value .
When your check is !myTextBox.value it checks that the value is not defined or not, it is equivalent to myTextBox.value == undefined .
Rito Jan 10 '19 at 6:12 2019-01-10 06:12
source share