I've got a User
entity in my system. Each user has a mail email (not relevant here) and an alternateEmails
property which is an array of strings (to have more multiple less important emails).
I need to create a form that will have the main email displayed always - and will have all alternate emails - 1 input for each alternate email. And a button add email
. Pretty standard interface thing in UI.
This is what I have now:
user-form.component.html
:
<div class="form-group" *ngFor="let ae of user.alternateEmails; let i = index">
<label for="alternateEmail{{i}}">alternate email {{i}}:</label>
<input class="form-control" name="alternateEmail{{i}}" id="alternateEmail{{i}}" placeholder="alternate email" type="text" [(ngModel)]="user.alternateEmails[i]" />
</div>
<button (click)="addAlternateEmailClicked()">add email</button>
user-form.component.ts
:
addAlternateEmailClicked(){
this.user.alternateEmails.push('');
}
It displays data very well. When I load data from the server, it reflects the number of alternate emails the user has (displays none if there were none, displays 2 if there were 2, etc):
When I click add email
, new input appears:
The crazy problem I'm facing is that when I focus on a given alternate email (click inside the input, so that I can start writing), I can type only one char - and the input loses focus. I get no idea why is focus/blur working behind the scenes. Perhaps it has something to do with angular magic.
I'd appreciate (1) how to change the code to make it usable :) (no loosing focus on dynamic inputs) and (2) explanation on why is current solution wrong.