简体   繁体   English

如何从表单数组和表单组函数中获取值..Angular

[英]How to get values from the form array and form group functions..Angular

https://www.tektutorialshub.com/angular/nested-formarray-example-add-form-fields-dynamically/ https://www.tektutorialshub.com/angular/nested-formarray-example-add-form-fields-dynamically/

the code is from here代码来自这里

    export class AppComponent  {

      title = 'Nested FormArray Example Add Form Fields Dynamically';

      empForm:FormGroup;


      constructor(private fb:FormBuilder) {

        this.empForm=this.fb.group({
          employees: this.fb.array([]) ,
        })
      }


      employees(): FormArray {
        return this.empForm.get("employees") as FormArray
      }


      newEmployee(): FormGroup {
        return this.fb.group({
          firstName: '',
          lastName: '',
          skills:this.fb.array([])
        })
      }


      addEmployee() {
        console.log("Adding a employee");
        this.employees().push(this.newEmployee());
      }


      removeEmployee(empIndex:number) {
        this.employees().removeAt(empIndex);
      }


      employeeSkills(empIndex:number) : FormArray {
        return this.employees().at(empIndex).get("skills") as FormArray
      }

      newSkill(): FormGroup {
        return this.fb.group({
          skill: '',
          exp: '',
        })
      }

      addEmployeeSkill(empIndex:number) {
        this.employeeSkills(empIndex).push(this.newSkill());
      }

      removeEmployeeSkill(empIndex:number,skillIndex:number) {
        this.employeeSkills(empIndex).removeAt(skillIndex);
      }

      onSubmit() {
        console.log(this.empForm.value);
      }


    }

This is template:这是模板:

    <form [formGroup]="empForm" (ngSubmit)="onSubmit()">

      <div formArrayName="employees">

        <div *ngFor="let employee of employees().controls; let empIndex=index">

          <div [formGroupName]="empIndex" style="border: 1px solid blue; padding: 10px; width: 600px; margin: 5px;">
            {{empIndex}}
            First Name :
            <input type="text" formControlName="firstName">
            Last Name:
            <input type="text" formControlName="lastName">

            <button (click)="removeEmployee(empIndex)">Remove</button>


            <div formArrayName="skills">

              <div *ngFor="let skill of employeeSkills(empIndex).controls; let 
 skillIndex=index">



                <div [formGroupName]="skillIndex">
                  {{skillIndex}}
                  Skill :
                  <input type="text" formControlName="skill">
                  Exp:
                  <input type="text" formControlName="exp">

                  <button (click)="removeEmployeeSkill(empIndex,skillIndex)">Remove</button>

                </div>

              </div>
              <button type="button" (click)="addEmployeeSkill(empIndex)">Add Skill</button>
            </div>


          </div>

        </div>
      </div>

      <p>
        <button type="submit">Submit</button>
      </p>

    </form>


    <p>
      <button type="button" (click)="addEmployee()">Add Employee</button>
    </p>

so, I need go get values of firstName, lastName from empForm and skill: '',exp: ''from, skills array...所以,我需要从 empForm 和 Skill: '',exp: ''from, Skills array 中获取 firstName、lastName 的值...

this function is not working for me to get values..这个功能不适合我获取值..

      get from_date() {
        return this.empForm.get("from_date");
      }

....thia is not working.. also not able to take values from skills array please help ....thia 不起作用.. 也无法从技能数组中获取值,请帮忙

When you submit the form, the structure of this.empForm.value could be described by the following interfaces.当您提交表单时,this.empForm.value 的结构可以通过以下接口来描述。

export interface Employee {
  firstName: string;
  lastName: string;
  skills: Skill[];
}

export interface Skill {
  exp: string;
  skill: string;
}

Where empForm.value could be described by the following structure:其中empForm.value可以由以下结构描述:

{
  employees: Employee[];
}

When you query the empForm on submit, you can get the data as if you're querying a regular object.当您在提交时查询empForm时,您可以像查询常规对象一样获取数据。

onSubmit() {
  const firstNames = this.empForm.value.employees.map(x => x.firstName);
  const firstEmployeeSkills = this.empForm.employees[0].skills.map(x => x.skill);
  // etc
}

As for this.empForm.get("from_date") , you don't have a property on the empForm called from_date , so this won't return anything.至于this.empForm.get("from_date") ,您在empFormempForm名为from_date的属性,因此这不会返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM