I have an array of objects structured like this
const premises = [
{
question: '1',
value: '1'
},
{
question: '2',
value: '2'
},
{
question: '3',
value: '3'
}
];
I am looking to generate form controls dynamically based on the data, similar to this
this.formBuilder.group({
1: [{ value: 1 }, [Validators.min(0), Validators.max(100), Validators.required]],
2: [{ value: 2 }, [Validators.min(0), Validators.max(100), Validators.required]],
3: [{ value: 3 }, [Validators.min(0), Validators.max(100), Validators.required]]
});
This is just a simplified example, there could be more values in the premises array.
I attempted something along these lines
this.formBuilder.group({
premises.forEach((x) => {
x.question: [{ value: x.value }, [Validators.min(0), Validators.max(100), Validators.required]]
});
});
However, I haven't been successful in creating dynamic controls and values.