Is there a way to display form values from an Observable using the reactive approach? I have a form with hard-coded data for this example and it displays my list of Labels in the HTML, but I am facing two issues I hope someone can spot that I'm not seeing/understanding.
- There is a TypeError: Cannot read property 'label' of undefined when using this.labels$[0].label as the initial form value for the field. I was hoping it would pull in the first label from my labels$() list.
- If I change the above to a string e.g. 'Test', the dropdown menu renders and I get the following
{"labels": "Test", "field1": "Some value", "field2": "Some value" }but when I change the dropdown menu, it removes the labels value completely.
{"field1": "Some value", "field2": "Some value" }
TS
get labels$() {
return [
{ "label": "label-1" }, // e.g. Mr.
{ "label": "label-2" },
{ "label": "label-3" },
{ "label": "label-4" }
];
}
HTML
<rx-select formControlName="labels" [options]="labels$ | async" [label]="Label"></rx-select>
Component.ts
return new FormGroup({
labels: new FormControl('', [Validators.required]),
field1: new FormControl('', [Validators.required]),
field2: new FormControl('', [Validators.required]),
});
Form
The ideal form value would be the following where I could see the values change with my interaction.
{"labels": "label-1", "field1": "Some value", "field2": "Some value" }
Any advice appreciated as I navigate the reactive form experience ;)
labels$ | async. But it's not: it's an array. You also use the namelabels, with a finals, to reference a single label. Very confusing again. Fix those errors, and post a complete minimal example as a stackblitz, explaining what you're doing, what you expect to happen, and what happens instead.