1

I am trying to bind input fields to a list of checkbox fields generated from API. As the code shows below, it will generate 7 checkbox field.

  <form [formGroup]="actForm">
                <mat-label class="activityCheckBox"
                  formArrayName="func1Activities"
                  *ngFor="let act of actForm.controls.activity.controls; let i = index;">
                  <tpcr-input-checkbox [formControlName]="i"></tpcr-input-checkbox>
                  {{activity[i].Name}}

                  <mat-form-field ngDefaultControl class="example-full-width" type="number">
                    <input matInput class="timeInput" type="number"
                      [(ngModel)]="formProfile.timeSpent" [ngModelOptions]="{standalone:true}">
                  </mat-form-field>

                </mat-label>
  </form>

Right now, the input fields are all generated along with the activity checkbox in the ngFor loop. Is there anyway to ONLY display the specific input field if one of the checkbox is selected?

For example, Activity 1 Checkbox = checked ----> Input Field Appears Activity 2 Checkbox = unchecked ---> Input Field Hide

This is bothering me for some time now, I really appreciate your time and help!!!

Thank you.

1 Answer 1

1

you has a form like

actForm=new FormGroup({
  activity=new FormArray([new FormControl(),new FormControl()]
})

So, you can access into the *ngFor like

actForm.get('activity').at(i).value

or

act.value

When you iterate over let control of actForm.get('activity').controls control is, if our formArray is a FormArray of FormControls the own control -if our formArray is a formArray of formGroup I like name as let group of actForm.get('activity').controls, so "group" is the formGroup

Note, if you only has an FormArray you need'n create a formGroup, you can directly

  activity:FormArray=new FormArray([new FormControl(),new FormControl()]

And

 <div *ngFor="let control of activity.controls;let i=index">
     <input [formControl]="control">
 </div>
Sign up to request clarification or add additional context in comments.

2 Comments

if I understand correctly, activity FormArray will contain two new FormControl() for each act? one is checkbox and the other one is input field? How would I toggle the input field based on the checkbox selected? thanks for your answer!
no, sorry, I explain bad. if a formArray with two elements, each one is a FormControl. The json value of actForm is {activity:['','']}. normally you has some like activity=new FormArray([]);activity.push(new FormControl());activity.push(new FormControl()) or activity=data.map(x=> new FormControl()) is an array of FormControls. Well really is a "super array" -has properties that the arrays has no

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.