Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从角度的动态形式中获取值_Angular_Typescript_Dynamicform - Fatal编程技术网

Angular 如何从角度的动态形式中获取值

Angular 如何从角度的动态形式中获取值,angular,typescript,dynamicform,Angular,Typescript,Dynamicform,如何从角度动力学形式中获取值 这是我的密码: 我的html 如何从ts文件的组数据中获取值您可以通过以下方法从表单组中获取单个数据值: this.DataForm.get(“”).value e、 g,如果您的formcontrol是“电子邮件”: this.DataForm.get('email').value 或者,您可以通过以下方式将整个表单值作为对象获取: this.DataForm.getRawValue()使用函数添加formControl import { Component, O

如何从角度动力学形式中获取值 这是我的密码: 我的html


如何从ts文件的组数据中获取值

您可以通过以下方法从
表单组中获取单个数据值:

this.DataForm.get(“”).value

e、 g,如果您的formcontrol是“电子邮件”:

this.DataForm.get('email').value

或者,您可以通过以下方式将整个表单值作为对象获取:


this.DataForm.getRawValue()
使用函数添加formControl

import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormArray, Validators, FormControl } from '@angular/forms';

@Component({
  selector: 'app-sample-reactive-form',
  templateUrl: './sample-reactive-form.component.html',
  styleUrls: ['./sample-reactive-form.component.css']
})

export class SampleReactiveFormComponent
{

  payLoad: any;
  FormData: any;
  DataForm: FormGroup = new FormGroup({});

  constructor()
  {
    this.FormData = [{
      name:'a'
    },{
      name:'b'
    }]

    this.DataForm = this.generateFormControls(this.FormData);
 }

    generateFormControls(formData: any)
    {
        let tempGroup: FormGroup = new FormGroup({});
        formData.forEach(i=>{
            tempGroup.addControl(i.name, new FormControl(''))
        })
        return tempGroup;
    }

    PreviewData() 
    {
         this.payLoad = JSON.stringify(this.DataForm.value);
    }
}

您应该使用
[formControlName]=“data.name”
而不是
name=“…”
表单控件名称不是来自数据库的固定名称,您永远无法猜出下一步显示的值是什么名称。
this.DataForm.getRawValue()
仍将返回一个对象,其中的值与键关联。无论
FormData
对象看起来如何。我现在遇到的问题是:如果我添加[formControlName]=“data.name”,则找不到名为valueName的控件,如果我删除[formControlName]:则会出现此错误
DataForm
的结构是什么?您是使用
FormBuilder
(反应式表单)创建的,还是尝试使用模板驱动的表单?根据您的代码片段,两者都没有正确实现。如果您正在构建
FormGroup
,则需要在模板中使用
formControlName
,如果您使用的是模板驱动,则需要
(ngModel)
。。。两者都没有。请使用formControlName而不是name属性。
PreviewData() {
    this.payLoad = JSON.stringify(this.DataForm.value);
}
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormArray, Validators, FormControl } from '@angular/forms';

@Component({
  selector: 'app-sample-reactive-form',
  templateUrl: './sample-reactive-form.component.html',
  styleUrls: ['./sample-reactive-form.component.css']
})

export class SampleReactiveFormComponent
{

  payLoad: any;
  FormData: any;
  DataForm: FormGroup = new FormGroup({});

  constructor()
  {
    this.FormData = [{
      name:'a'
    },{
      name:'b'
    }]

    this.DataForm = this.generateFormControls(this.FormData);
 }

    generateFormControls(formData: any)
    {
        let tempGroup: FormGroup = new FormGroup({});
        formData.forEach(i=>{
            tempGroup.addControl(i.name, new FormControl(''))
        })
        return tempGroup;
    }

    PreviewData() 
    {
         this.payLoad = JSON.stringify(this.DataForm.value);
    }
}