I am trying to wrap my head around this problem for some time now.
When I try to have an input in a form only if a condition is true, I receive an error, that invalid could not be read from undefined.
When using the elvis operator I don't get the error anymore, but even if the input is displayed, invalid & dirty, I still don't see the error message.
<form #myForm="ngForm">
... other inputs ...
<input *ngIf="model.type === 'V'"
name="price"
type="number"
required
[(ngModel)]="model.price"
#price="ngModel">
<div class="errors" *ngIf="price?.invalid && price?.dirty">
Problem detected
</div>
</form>
Anyone a hint what I am missing?
What I would do , I'd wrap the input and the error inside the first condition :
This makes more sense , because you don't want the check for the error to fire if the input does not exist at all !
And the other thing is , the only validation you have is required , so as soon as you make this field dirty, it becomes valid, unless you delete your text.