How to set id for each filed in form group?

109 views Asked by At

I'm using angular and I need check id of each input. Is there any way to set id form each form input like this?

this.contactForm = this.formBuilder.group({
  firstname: ['', {id: 1}],
  lastname: ['', {id: 2}],
  email: ['', {id: 3}]
)}
1

There are 1 answers

3
Joep Kockelkorn On

Yes you can, but you shouldn't use the FormGroup configuration for this. FormGroup has no support for setting the id attribute.

You can assign the id attribute of an input like this:

<input [id]="'my-id'" />

So you have to either hardcode the ids into the template, or use some other form of configuration than FormGroup to get it from like a dictionary object.