One interesting feature of my form is the dynamic input field where users can easily add more fields by simply clicking on a button. These input fields are then linked to the template using ngFor, as shown below:
*ngFor="let data of getTasks(myFormdata); let i=index"
In addition, here is the corresponding code from the ts file:
getTasks(myFormdata) {
return myFormdata.get('inputs').controls
}
The functionality works smoothly in allowing users to add new input fields. However, I have encountered an issue with a button that is supposed to assign a random number to each input field. As I am relatively new to Angular 2, I am struggling to implement this feature when there are multiple input fields present. The method responsible for generating and assigning the random number is outlined below:
getRandomNumber() {
const random = Math.floor(Math.random() * (999999 - 100000)) + 100000;
const control = <FormArray>this.myFormdata.controls['inputs'];
control.setValue([{numbers: random, pari: 25}])
}
I would appreciate some guidance on how to modify the getRandomNumber()
method so that it generates a unique random number for each additional field that is added.