Home > Software design >  ReactiveForm : ValuesChanges is not defined?
ReactiveForm : ValuesChanges is not defined?

Time:07-28

my concern is that I get a valueChanges undefined in a reactiveForm. It starts to make a moment that I struggle on this stuff

Here is the form with the formControl "treasuryTime" (I removed other FormControl without interest):

// Form controls
    this.form = this.formBuilder.group({
      treasuryTime: [null, [Validators.required, CustomValidators.requiredMatch]],
      pensionFund: ['', [Validators.required]],
    });
  }

the valueChanges in question:

this.form.controls['treasuryTime'].valueChanges.subscribe((x: IdentifiantLibelleDto) => {
        if (x) {
          console.log("DELAI TRESORERIE ------  "   x.libelle)
          // return undefined.....
        }
      }),

I tried the syntax without success;

  this.form.get('treasuryTime').valueChanges.subscribe((x: IdentifiantLibelleDto) => {
  this.form.controls.['treasuryTime'].valueChanges.subscribe((x: IdentifiantLibelleDto) => {.....

In the html this is a select type element and I get all the items of the list, so no worries at this level.

<div  [ngClass]="{'error' : this.form.controls.treasuryTime.invalid && this.form.controls.treasuryTime.touched}">
        <select [attr.aria-label]="page   '.treasuryTime' | translate" required formControlName="treasuryTime">
          <option value="" selected>{{ page   '.duration' | translate }}<span >*</span></option>
          <option *ngFor="let t of treasuryTimes" [value]="t">{{ t.libelle }}</option>
        </select>
        <span >{{ 'error.required' | translate }}</span>
      </div>

Thanks for your help

CodePudding user response:

Are you sure you already import ReactiveFormsModule?

CodePudding user response:

I found my answer...

In HTML I couldn’t retrieve the object with the binding I used [value]="myObject"

Instead, must be done : [ngValue] = "myObject"

Hope it will help

  • Related