Sobright
Sobright

Reputation: 241

Angular How to get value in form array?

I want to get 'onduty' values from all form array's instance. I tried this in html:

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

but I couldn't get any value.

this is workDayandTime's value in json : workDayandTime form array

Form :

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>

I dropped other code but about formarray, What I wanna know is How can I get each instance's 'onduty' value in array. Sorry for my short of Eng. But if you help to solve this problem. It'll be big pleasure for me. thanks.

Upvotes: 0

Views: 19061

Answers (3)

the code above worked for me. follow other exemple.

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

Upvotes: 1

Sobright
Sobright

Reputation: 241

I solved this,

in HTML :

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

this is form array's structure : Structure

It's quite complicated : <

Upvotes: 6

Rahul Dapke
Rahul Dapke

Reputation: 355

Just use workDayandTime instead of workDayandTime.controls. It will work :

    <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>

Upvotes: 1

Related Questions