Home > Back-end >  .get() is not working for an object in TypeScript
.get() is not working for an object in TypeScript

Time:04-25

.get() is not working for an object in TypeScript. In HTML I am unable to access the max value of the dataFormparams object.

export interface FormBUilderParams extends Object {  
    required: boolean;
    minlength: number;
    maxlength: number;
    regx: string
  }
 dataFormparams!: FormBUilderParams;

 Object.defineProperty(this.dataFormparams, "element.controllName", {
   required: element.required,
   minlength: element.minlength,
   maxlength: element.maxlength,
   regx: element.regx
   });
<mat-hint align="start">Min {{dataFormparams.get('name').minlength}} and Max {{dataFormparams.get('name').maxlength} Characters</mat-hint>

CodePudding user response:

You can use this way

reactiveForm: FormGroup;

this.reactiveForm = this.formBuilder.group({
    email: [null, [
        Validators.required,
    ]
});

// convenience getter for easy access to form fields
get f() { return this.reactiveForm.controls; }

CodePudding user response:

My guess is that element.controllName has a value of "name", and you're trying to access that property. It would be like this:

Lose the quotations

 Object.defineProperty(this.dataFormparams, element.controllName, {
   required: element.required,
   minlength: element.minlength,
   maxlength: element.maxlength,
   regx: element.regx
   });

Square brackets to look for a property given a string, question mark to prevent undefined errors.

<mat-hint align="start">Min {{dataFormparams['name']?.minlength}} and Max {{dataFormparams['name']?.maxlength} Characters</mat-hint>
  • Related