Angular如何在表单数组中获取价值?

时间:2018-07-24 02:11:35

标签: angular forms typescript formarray

我想从所有表单数组的实例中获取“义务”值。 我在html中试过了

<span *ngFor="let day of workDayandTime.controls;let i= index;">
 {{day.onduty}}
</span>

但我没有任何价值。

这是json中workDayandTime的值: workDayandTime form array

表格:

class employerShortForm extends FormGroup {
    constructor(fb: FormBuilder) {
        super({
            employer: fb.group({
                name: new FormControl('',Validators.required),
            }),
            employee: fb.group({
                name: new FormControl('', Validators.required)
            }),
            contractStart: new FormControl(''),
            contractEnd: new FormControl(''),
            workAddress: new FormControl('', Validators.required),
            work: new FormControl('', Validators.required),
            workDayandTime: fb.array([]),
            wageType: new FormControl(''),
            wage: new FormControl(''),
            bonus: new FormControl(''),
            otherWage: fb.array([]),
            otherwageRadio: new FormControl(''),
            overTimeAddRate: new FormControl(''),
            payDateType: new FormControl(''),
            payDate: new FormControl(''),
            payMethod: new FormControl(''),
            insurance1: new FormControl(''),
            insurance2: new FormControl(''),
            insurance3: new FormControl(''),
            insurance4: new FormControl(''),
            company: fb.group({
                name: new FormControl('', Validators.required),
                phone: new FormControl('', Validators.required),
                address: new FormControl('', Validators.required),
            }),
            ownerName: new FormControl('', Validators.required),
            employeeEmail: new FormControl('', Validators.required),
        });
    }

}

short-time-paper.ts

import { employerShortForm, employeeContForm, DataHandling, otherWage, workDayandTime} from '../../../contract-form'
import { FormGroup, FormControl, FormBuilder, FormArray } from '@angular/forms';
import { Component, AfterViewInit, ElementRef, Renderer2, ViewChild } from '@angular/core';
import { IonicPage, ViewController, ModalController, NavParams, Alert, NavController, Checkbox, Platform } from 'ionic-angular';

@Component({
  selector: 'app-short-time-paper',
  templateUrl: 'short-time-paper.html',
})
export class ShortTimePaperComponent implements AfterViewInit{
  public employerForm: employerShortForm = new employerShortForm(this.fb);
  ngAfterViewInit() {
    this.addworkDayandTime();
  }
get workDayandTime(): FormArray{
    return this.employerForm.get('workDayandTime') as FormArray;
  }
  
  public addworkDayandTime() {
    for (let i = 0; i < 7; i++) {
      this.workDayandTime.push(this.fb.group(new workDayandTime()));
    }
  }
}

short-time-paper.html

    <form [formGroup]="employerForm">
          <span *ngFor="let day of workDayandTime.controls;let i= index;">
            {{day.onduty}}
            <span *ngIf="day.onduty=='false'">{{getDays(i)}},&nbsp;</span>
          </span>
    </form>

我删除了其他代码,但是关于formarray, 我想知道的是如何获取数组中每个实例的“ onduty”值。 对不起,我缺英文。但是如果您帮助解决这个问题。对我来说,这将是极大的荣幸。谢谢。

3 个答案:

答案 0 :(得分:2)

只需使用 workDayandTime 而不是 workDayandTime.controls 。 它将起作用:

    <form [formGroup]="employerForm">
          <span *ngFor="let day of workDayandTime;let i= index;">
            {{day.onduty}}
            <span *ngIf="day.onduty=='false'">{{getDays(i)}},&nbsp;</span>
          </span>
    </form>

答案 1 :(得分:2)

我解决了这个问题,

在HTML中:

<span *ngFor="let day of workDayandTime.controls;let i= index;">
  {{ day.controls.onduty.value }}
</span>

这是表单数组的结构: Structure

这很复杂:<< / p>

答案 2 :(得分:0)

上面的代码对我有用。遵循其他示例。

 <div *ngFor="let rules of condition['controls'].filters['controls']; let conditionIndex = index" class="row">{{rules.controls.formId.value }}</div>