Retrieving Data from Repeated Component in Angular 6

Need Help with Reading Values from Repeating Control in Angular 6

I am struggling to retrieve the value of a form field in the TS file. Can someone please assist me with this?

This section contains repeating blocks where you can click "add" and it will generate another control group.

<div *ngIf="subTask.value==5 && subTask.value!=null" formArrayName="itemRows">
        <div *ngFor="let itemrow of taskfrm.controls.itemRows.controls; let i=index" [formGroupName]="i">
          <!-- Border START -->
          <div class="add-new-task-border">
            <mat-form-field class="example-full-width">
              <mat-select placeholder="Logical Operator">
                <ng-container *ngFor="let relationaOpt of relationalOpraters">
                  <mat-option *ngIf="relationaOpt.operatorsType==='logical'" [value]="relationaOpt.id">
                    {{relationaOpt.operator}}
                  </mat-option>
                </ng-container>
              </mat-select>
            </mat-form-field>

            <mat-form-field class="two-way-banding">
              <mat-select placeholder="DataElement 1">
                <mat-option *ngFor="let dataElement of dataElementList" [value]="dataElement.dataElementID">
                  {{dataElement.dataElementName}}
                </mat-option>
              </mat-select>
            </mat-form-field>

            <mat-form-field class="two-way-banding eta-margin-two-way">
              <mat-select placeholder="Relational Operator">
                <ng-container *ngFor="let relationaOpt of relationalOpraters">
                  <mat-option *ngIf="relationaOpt.operatorsType==='relational'" [value]="relationaOpt.id">
                    {{relationaOpt.operator}}
                  </mat-option>
                </ng-container>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="two-way-banding">
              <mat-select placeholder="DataElement 2">
                <mat-option *ngFor="let dataElement of dataElementList" [value]="dataElement.dataElementID">
                  {{dataElement.dataElementName}}
                </mat-option>
              </mat-select>
            </mat-form-field>
          </div>
          <!-- Border END -->
        </div>
      </div>
      <button *ngIf="subTask.value==5 && subTask.value!=null" style="float:right;" type="button" mat-icon-button color="primary"
        (click)="addNewRow()">
        <mat-icon>add_circle</mat-icon>
      </button>
      <button *ngIf="subTask.value==5 && subTask.value!=null" style="float:right;" type="button" mat-icon-button color="primary"
        (click)="addNewRow()">
        <mat-icon>remove_circle</mat-icon>
      </button>

If you have any insights on this matter, please share them with me.

Answer №1

In order to effectively manage your form array, it is important to keep track of the index:

form.controls.itemRows.controls[i].controls.name.value

Here is the corresponding HTML code:

<div *ngIf="subTask.value==5 && subTask.value!=null" formArrayName="itemRows">
<div *ngFor="let itemrow of taskfrm.controls.itemRows.controls; let i=index" [formGroupName]="i">

    <mat-form-field class="col-md-4">
        <input matInput maxlength="20" 
          formControlName="name">
    </mat-form-field>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I display a new module in Angular without navigating to it?

After following the tutorial at https://angular.io/guide/lazy-loading-ngmodules#create-a-feature-module-with-routing I set out to create the following: My goal is to have a dedicated module for all customer-related components accessible through the /cust ...

Avoid duplicating content when using Ajax to retrieve data in jQuery

Is there a solution when you click the button to display content, then click the button to hide it, and then click the button again to show it repeatedly? I'm not sure how to solve this issue. Can anyone help me out? Thank you! Here is the HTML code: ...

What is the best method for inserting the HTML content from a specific div into a textarea?

Everything seems to be working fine, but once I insert the HTML into the textarea, an issue arises where the div gets wrapped within another div, causing the layout to break. var urls = []; $('body').on('click', '.btn_video&apos ...

What is the process for integrating additional Firebase Cloud Functions into an Angular Universal project?

When working on an Angular Universal project, the fixed configuration for firebase.json looks like this: { "hosting": [{ "target": "PROJECT-ID", "public": "dist/PROJECT-ID/dist/PROJECT-ID/bro ...

How can you cycle through multi-dimensional arrays in Angular 2?

UPDATE#2 Unfortunately, I've been unable to successfully implement either of the suggestions provided. It's getting late, so I'm going to call it a night and revisit this tomorrow. A special thanks to Pradeep Jain for offering assistance. ...

Issue observed: React Map layer is not loading until mouseEnter event happens

The map is displayed with the color fill only when a mouse enter event occurs. How can I make it trigger on load instead? I am working with react-simple-maps, and the JSON data is successfully loading the map on mouse enter. You can find the source code ...

Tips for retrieving JSON data from an AJAX call and displaying it pre-filled in an input field

Here is the code snippet I used to receive a response in JSON format, but when I try to display the response using alert(response.Subject);, it shows as "undefined". HTML: <input type="text" id="subject" value='Subject'> Javascript: $.a ...

multiple simultaneous ajax calls being triggered

I am currently working on creating 4 cascading dropdowns, where the options in each dropdown repopulate based on the selection made in the previous one. To achieve this functionality, I decided to implement an Ajax call. The call takes the parameters, det ...

When you try to import from another file, the method is not defined

When I attempt to import a method from another file, I am encountering an issue where it returns undefined. The structure involves 3 files with one calling the next in sequence. File1: const { methodFromFile2 } = require('./file2'); methodFromFi ...

Showing content from a JavaScript variable once a button has been clicked

Imagine you are using express.js and have a JavaScript variable embedded in an ejs file like this: <%= someVariable %> How can you display the value from this variable on the same page, for instance within a bootstrap modal element (check out https: ...

The Lightbox containing an IFrame is experiencing flickering issues when links are clicked

I am experiencing a similar issue with this post: How can I resolve flickering in IFrames? Unfortunately, I have yet to find a solution (and I'm worried about receiving negative ratings too :) ). Due to the fact that my website is on an intranet, I ...

Exploring JavaScript Regular Expressions in Internet Explorer 11

I am attempting to eliminate all non-digit characters from a string using JavaScript. While this code works properly in FF and Chrome, it does not work in IE11 and no characters are removed. var prunedText = pastedText.replace(/[^\d\.]/g,""); ...

Integrating Node.js with static HTML documents

I'm currently exploring methods for making node.js API calls from static HTML. While I've considered using Express and similar template engines, I am hesitant since they would require me to adjust my existing HTML to fit their templates. Typicall ...

The process of obtaining points through accurate responses using form inputs

My task is to create a unique quiz consisting of 10 questions. Half of the questions are multiple choice, which require radio inputs, while the other half are written answers that need text inputs. To ensure accuracy and provide a scoring system, I came ac ...

Leveraging jQuery to interact with MySQL database in PHP Laravel framework

I seem to have hit a roadblock with my database, which is quite intricate. Here's the breakdown of the tables used for the questions: product - contains product details such as shoes productattribute - houses different variations of products like bl ...

Inconsistency in @nuxtjs/i18n locales after refreshing the page

I am currently working on an application where I am implementing language management, but I have encountered a difficulty. I am using the latest version of @nuxtjs/i18n. Changing the language successfully updates the URL and labels, yet upon refreshing the ...

What is a more efficient way to write nested subscribe in Angular?

I am a beginner with RxJS and I'm interested in learning how to write clean code using it. I currently have a nested subscription that I've been trying to refactor without success. firstMethod() { this.testMethod(name) console.log(this.curren ...

Tips for showing user data following form submission to a SQL database using React JS

Hey everyone, I'm currently working on a project that involves user registration and login. Once the users complete these steps, they are required to fill out an additional form which is displayed below. After submitting this form, the data is stored ...

Charting Add-Ons for jQuery

Looking for suggestions on user-friendly graph drawing plugins compatible with jQuery. Currently developing a web application that will retrieve data from a remote database and present it through visual graphs... Explored jgcharts (the jQuery Google Chart ...

iOS Safari browser does not support changing the caret color in textarea

Seeking a solution to hide the text cursor (caret) from a textarea on iOS browsers like Safari and Chrome. Despite trying the caret-color property, it does not seem to work. Are there any alternative methods to achieve this? One approach I attempted is b ...