0

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.

  1. 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.
  2. 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 ;)

4
  • 1
    By convention, suffixing a variable with '$' is done when this variable is an Observable. It seems that it should be an observable, since you're using labels$ | async. But it's not: it's an array. You also use the name labels, with a final s, 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. Commented Jul 15, 2019 at 16:29
  • what is rx-select?? Commented Jul 15, 2019 at 17:48
  • @Eliseo - rx-select is a directive. Commented Jul 16, 2019 at 12:31
  • @JBNizet - yes $ is associated with an Observable but for this example, the data was hardcoded array which is what my observable object is when complete. As for labels, it is as is. i.e. a list of labels! Tnx Commented Jul 16, 2019 at 12:33

2 Answers 2

1

If you want to just view the value of a FormGroup, then you can do it in 2 ways:

  1. Print the value for FormGroup instance in the HTML, with async pipe. json pipe is needed to format the object into text.

In the HTML file:

formGroupInstance.valueChanges | async | json
  1. Print the value for FormGroup instance in the Console, by subscribing to the valueChanges.

In the TypeScript File:

formGroupInstance.valueChanges.subscribe(value => console.log(value));

You can find an example HERE | stackblitz.com

Sign up to request clarification or add additional context in comments.

Comments

0

Found the issue. The json object needs a value in order to render correctly in the directive dropdown. So changing { "label": "label-1" } to { "label": "label-1", "value": "value1" } works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.