JvB
JvB

Reputation: 297

Numeric value shows as empty string when accessing formGroup values?

Can someone explain why the numeric values show as an empty string within my formGroup,

  private formatFormValues(depositDates) {
    return depositDates.map((depositDate) => {
      console.log('deposit dates', depositDates);
      console.log('deposit dates - amount', depositDate.controls.effectiveDates.value);
      return {
        effectiveDates: depositDate.controls.effectiveDates.value,
        depositDate: depositDate.controls.depositDate.value,
      };
    });
  }

I have this depositDates formGroup instance which Im logging and looks like this: enter image description here

I then try to access the values within this instance, and for some reason the amount always shows as an empty string despite it being shown as an active and set numeric value?

enter image description here

Am I in some what missing something / accessing something incorrectly for this to happen?

Upvotes: 1

Views: 61

Answers (3)

Paul
Paul

Reputation: 478

Maybe the method returns values before the map function finishes? You can try the code below..

async formatFormValues(depositDates) {
        return new Promise(async (resolve, reject) => {
            resolve(depositDates.map(async (depositDate) => {
                console.log('deposit dates', depositDates);
                console.log('deposit dates - amount', depositDate.controls.effectiveDates.value);
                return {
                    effectiveDates: depositDate.controls.effectiveDates.value,
                    depositDate: depositDate.controls.depositDate.value,
                };
            }));
        });

    }

When you access it you can use:

this.formatFormValues(depositDates).then(resp => {
    // set form values
});

Upvotes: 2

JvB
JvB

Reputation: 297

Turns out I had an option param in my setValue() call stopping any event emitting, still dont understand why it would access the amount value despite it being there prior to my changes, answer goes to anyone who can explain..

Upvotes: 0

Julien Ambos
Julien Ambos

Reputation: 2088

Sometimes, Angular is not being notified to update the value of a form group. You can manually call updateValueAndValidity on your form group or the required form control.

Upvotes: 1

Related Questions