I'm using model driven forms in an Angular 7 application, and one of them allows the user to add/delete a group of fields dynamically. Here's a visual reference of what I mean:
This is the FormModel I use:
this.formModel = new FormGroup( {
id : new FormControl(''),
name : new FormControl('', Validators.required),
comment : new FormControl(''),
options : new FormControl(3),
domain_users: new FormArray([this.createDomainUser()])
});
The dynamic property is domain_users
, which is a FormArray
of FormGroup
with 3 FormControl
(domain, username and password). When the user clicks the add button, this is what I do:
let du = this.formModel.get('domain_users') as FormArray;
if(nodes) {
nodes.push(this.createDomainUser());
}
I'd like that, when the user clicks the add button, the focus moves to the 'domain' field of the newly created row. What would be a good 'Angular way' of doing this?
Thanks in advance,
This is the solution I found:
In the HTML template:
In the component source: