Issue: Unable to locate control using path 'users -> 0 -> data -> cat_name'

I have been struggling to display the cat_name in a text box, but so far I haven't been successful. Here is my code snippet:

constructor(private fb: FormBuilder) { 

this.myForm = this.fb.group({
users: this.fb.array([])
})

let dataArr = new FormArray([]);
dataArr.push(new FormGroup({
'cat_name': new FormControl(this.users[0].data[0].cat_name),
'category': new FormControl(this.users[0].data[0].category)
}));

let formArr = <FormArray>this.myForm.controls.users;
formArr.push(fb.group({
firstname: this.users[0].firstname,
lastname: this.users[0].lastname,
street: this.users[0].street,
data: dataArr
}));
}
<fieldset formGroupName="data">
cat_name:<input type="text" formControlName="cat_name">
</fieldset>

The firstname and lastname successfully render in a text box, however, when it comes to the "data-sub array" which includes the cat_name, I have not been able to achieve the same results. Here is the URL to my code for reference: Code URL

I have even attached a screenshot showing that I am unable to render the cat_name field in the textbox Image. Below you can find the full HTML code:

<form [formGroup]="myForm" (ngSubmit)="onMy(myForm.value)">  
<ng-container formArrayName="users">
<div *ngFor="let user of myForm.controls.users.controls; let i=index" [formGroupName]="i">

<select (change)="selectarch($event)">
<option>Select</option>
<option [value]="res.firstname" *ngFor="let res of users">{{res.firstname}}</option>
</select>   
<br><br>

First name:<input type="text" formControlName="firstname">
<br>
Last name:<input type="text" formControlName="lastname">
<br>
Last name:<input type="text" formControlName="street">
<br><br>

<select (change)="selectcolumn($event)">
<option>Select</option>
<option [value]="col.cat_name" gFor="let col of test[0].data">{{col.cat_name}}</option>
</select>    
<br><br>
<fieldset formGroupName="data">
cat_name:<input type="text" formControlName="cat_name">
</fieldset>

</div>
</ng-container>
<button type="submit">Add</button>
</form>

Answer №1

It appears that the data is stored as a formArray instead of a formGroupName. To properly access and iterate through the data, you should loop through the data formArray.

  <fieldset formArrayName="data">
    <div *ngFor="let d of user.controls.data.controls; let ik=index" [formGroupName]="ik">
    cat_name:<input type="text" formControlName="cat_name">
    </div>
    </fieldset>

Here's a working example for reference.

Answer №2

Starting with ReactiveForm can be challenging yet rewarding. An easier alternative to consider is the Template-Driven Form Approach.

The issue at hand is that you are attempting to access a second formArray within a formArray without proper declaration.

You have declared formArrayName="users" and iterated through it using

let user of myForm.controls.users.controls
. However, you also need to iterate through users' data.controls.

For each level of FormArray, you must declare a formArrayName.

<form [formGroup]="myForm" (ngSubmit)="onSubmit()">

<div formArrayName="users">
  <div *ngFor="let userCtrl of myForm.get('users').controls; let j = index;"
       [formGroupName]="j" >
    <div formArrayName="data">
      <fieldset *ngFor="let dataCtrl of userCtrl.get('data').controls; let i = index;"
                [formGroupName]="i">
        cat_name:<input type="text"  formControlName="cat_name">
      </fieldset>
    </div>

  </div>
</div>
</form>

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

Tips for implementing react-select types in custom component development

Currently, I'm in the process of developing custom components for DropdownIndicator to be used on react-select with Typescript. However, I am encountering difficulties with the component's type due to my limited experience with Typescript. I wou ...

Exploring the method of dynamically adding a row to a table on button click using Angular 2

In the midst of my Angular2 project, I am exploring the possibility of dynamically adding rows to a table when a button is clicked. While I am aware that *ngFor can achieve this, I am curious if it can be implemented only upon the clicking of a button. ...

An improved method for implementing conditional statements in Angular

After researching online, I came across some discussions about using if else logic in Angular. Although I was able to achieve the desired outcome, I am curious if there is a more efficient or alternative way to implement if else statements in Angular. In ...

Encountering a problem when making a HTTPS GET request to a REST API using

I am currently running an Angular application that utilizes an external API to fetch country ISOs. However, I am encountering an error with the API since it uses HTTPS. Interestingly, when I implement a proxy in my Angular local environment by mapping /is ...

Addressing ESLint and TypeScript Issues in Vue.js with Pinia: A comprehensive guide

Experiencing difficulties with Vue.js + Pinia and need assistance to resolve these issues. Error: 'state:' is defined but never used. Here is the snippet of code located in @/stores/user.ts. import { defineStore } from 'pinia' export ...

Getting started with Angular2 ngrx storemodule reducer - Understanding the process of injecting services into the initialState

const startingState = { // service.fetch(); } How can we inject a service into a reducer? Is it possible to utilize a service within a reducer without a constructor or class? export function personnelHandler(state = startingState, action: PersonnelAction ...

Adding text to a text area that is created in React JS is simple with the use of the

I'm currently working with some code that involves creating a text area component. interface IReceiverProps { receivedMessage: string; topic: string; } export default class Receiver extends React.Component<IReceiverProps, {}> { re ...

The conversion to ObjectID did not succeed due to an issue on the front end

I have encountered an issue while trying to send a post request to create a task within a list using an API provided to me. This error occurs when I attempt to make the request in postman. Below is the schema of the post request body required by the API, ...

Issue with ToggleButtonGroup causing React MUI Collapse to malfunction

I'm having trouble implementing a MUI ToggleButtonGroup with Collapse in React. Initially, it displays correctly, but when I try to hide it by selecting the hide option, nothing happens. Does anyone have any suggestions on what might be causing this ...

Developing a custom web component using Angular 6

Hello, I'm facing a challenge with my Angular 6 application. I need to create a web component that can be integrated into another web application. I followed a tutorial and customized it according to my requirements which you can check out here: Howe ...

Error message for invalid form control not displayed when value is assigned to form control in Angular

I am currently working with this editor: <div id="editor"></div> which sets its value to a form control. this.FrmGroup.get("name").setValue(this.quill.root.innerHTML)` <div *ngIf="FrmGroup.get('name').inv ...

Ways to verify if an entire FormGroup is invalid, not just if a single FormControl is invalid

When using Angular with ReactiveForms, what is the best way to determine if the entire FormGroup is invalid? ...

Creating a Proxy for an Array in TypeScript: A Step-by-Step Guide

When attempting to utilize a Proxy for adding logic whenever a value in my array is set to a new value, I encountered an issue with TypeScript. The error arises from the fact that TypeScript does not support indexing an array with anything other than a num ...

Invoking Component Function via Service

I am in the process of developing a file upload feature for my application. Once the files are successfully uploaded, I want to clear the list of uploaded files. To achieve this, I have created the following function in the file uploader component: clea ...

Ways to troubleshoot opencv.js generating random values when applying cv.threshold

Whenever I choose an image and use cv.threshold, it outputs a strange number 6620912 The number seems to change at times https://i.sstatic.net/Tp9LP.png 6620912 Promise.catch (async) (anonymous) @ App.tsx:49 commitHookEffectListMount @ react-dom_client ...

What exactly is the concept behind the Strategy OnPush?

I am a beginner in the world of Angular2 with TypeScript. As I delve into my project, one concept that continues to elude me is the purpose of OnPush: changeDetection : ChangeDetectionStrategy.OnPush Despite my dedicated efforts in researching this top ...

ngModel is ineffective when using primeng table alongside a native checkbox

Currently, I am utilizing a primeng editable table that includes native checkboxes and ngModel. <p-table id="resultTable" [columns]="cols" [value]="results" [rowTrackBy]="trackByFunction" scrollable="true" selectionMode="single" [selection]="selected" ...

Angular 2 and Internet Explorer 11 seem to be having a disagreement over the definition of the property 'webpackJsonp'. It appears that IE11 believes it is null or undefined

Hello everyone! I recently started learning Angular 2 and I successfully installed Angular-CLI by following the instructions on https://github.com/angular/angular-cli. After that, I created a sample project to practice with. After building the app using " ...

Using Angular2 in conjunction with the simpleheat plugin

I've been attempting to integrate the NPM plugin Simpleheat () into my Angular2 app, but unfortunately, the heatmap is not rendering even though everything appears to be set up correctly. You can find the full repository with the issue here: https:// ...

The utilization of the Angular date pipe significantly impacts the way dates are

When I use the pipe date:'MM/dd/YYYY' to display the date 2022-01-01T00:00:00, it shows as 1/01/2021 instead of 1/01/2022. This issue only occurs with this specific date. Why does this happen? The value of pharmacyRestrictionDate is 2022-01-01T0 ...