user10745471
user10745471

Reputation:

angular6: How to display value of form

I want to display only information from the form that controlname is in myObject.

this.form = this.FormBuilder.group({
  lname:[null, Validators.compose([Validators.required])],
  fname: [null, Validators.compose([Validators.required])],
  phone:[null, Validators.compose([Validators.required])],
  id:[null, Validators.compose([Validators.required])],
  age:[null, Validators.compose([Validators.required])],
});

I only want to display the values ​​in this object

this.myObject = {0:"id", 1:"fname", 2:"lname"}



for (var val in this.myObject ) {
console.log(this.form.value.myObject [val] )

}

Upvotes: 1

Views: 57

Answers (2)

Lia
Lia

Reputation: 11982

you can try:

for(let item in this.myObject){
      console.log(this.form.controls[this.myObject[item]].value) 
      console.log(this.form.get(this.myObject[item]).value) // or
      console.log(this.form.value[this.myObject[item]]) // or
   }

Upvotes: 1

web.dev
web.dev

Reputation: 359

Inside for loop: this.form.get(val).value

FormGroup.get(controllerName: string) returns AbstractController

Upvotes: 0

Related Questions