angular6:如何显示表格的值

时间:2018-12-15 06:00:18

标签: angular forms angular5 angular-reactive-forms

我只希望显示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])],
});

我只想显示该对象中的值

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



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

}

2 个答案:

答案 0 :(得分:1)

您可以尝试:

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
   }

答案 1 :(得分:0)

内部for循环: this.form.get(val).value

FormGroup.get(controllerName: string)返回AbstractController