0

imageIn ref to this question https://stackoverflow.com/questions/57707339/how-to-dynamically-use-ng-for-to-get-input-boxes-and-checkbox/57707843?noredirect=1#comment101860409_57707843 . I am able to dynamically generate div elements(comprises of input element and checkbox).

I am able to retrieve the values. But my problem is: input elements must be stored along with checkboxes that are clicked. Account(input elements) have different roles associated with them. Roles are assigned based on the checkboxes checked values.

In typescript, I want to store account and its roles together. In Angular code, If I have two accounts, I am able to fetch the values of first account values and not the second value. How to I modify ngFor to fetch multiple accounts values.

Here is the code snippet and what I have tried

Angular.html

<form (ngSubmit)="SaveRecord()" #idForm="ngForm">

 <div *ngFor="let item of record.roles;let i=index">
  <label for="name">AccountId</label>
  <input type="text" class="form-control" [(ngModel)]="item.accountid" name="accountid" (change)="updateUser($event.target.value)" required> 

 <label>IT Admin</label>  
 <mat-checkbox [(ngModel)] = "item.ITrole"  id="check2" [checked]="isChecked"  name="ITcheck" (change)="checkBoxClick($event,'IT Admin')"> 
 </mat-checkbox>
 </div>


typescript.ts

  record = {firstName:null,lastName:null,emailAddress:null,accountRoles:1,roles:[{accountid:null,role:null}]};             
SaveRecord(){
   //what should I write here to get input and checkbox values and store them in array
  }
4
  • You have only one role: roles:[{accountid:null,role:null}]. You are looping through roles not accounts. Commented Aug 30, 2019 at 14:17
  • Thats correct , I will modify the object model to roles:[{accountid:null,role:[]}] Commented Aug 30, 2019 at 14:19
  • Before modifying. Do you want to show input for each account or for each role of the account? Commented Aug 30, 2019 at 14:20
  • I want to show the inputs for accounts and its associated roles ( checkboxes that are checked) Commented Aug 30, 2019 at 14:21

1 Answer 1

0

I suggest to use Nested FormArrays.

The following might not be a complete answer but can help you to refactor and fulfill your needs:

Add the following to your logic:

records = [];
roles = ['Admin', 'IT'];

changeRecords(e) {
   const record = [{accountId: null, roles: [{role: 'Admin'}, {role: 'IT'}]}];
   this.records.push(record);
}

And your HTML:

<form>
  <input type="number" (change)="changeRecords($event)">
  <div *ngFor="let record of records">
    <label>Account ID</label>
    <input type="text"/>

    <div *ngFor="let role of roles">
      <label>{{role}}</label>
      <input type="checkbox"/>
    </div>
  </div>
</form>
Sign up to request clarification or add additional context in comments.

Comments

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.