An error occurs when trying to access the 'controls' property within the nested form of an Angular form as it is null

Just getting started with angular and tackling the challenge of creating a nested form. I'm struggling to access controls for certain fields based on the JSON structure provided below, specifically having trouble with the assetsList -> description section.

data = {
    headline: [
      {
        language: 'en',
        headlineText: 'example headline',
      },
    ],
    bodyText: [
      {
        language: 'en',
        bodyText: 'example bodytext',
      },
    ],
    location: {
      name: 'mkontheway',
      openingHours: [
        {
          day: 'Mon-frd',
          timing: '10.00PM-9AM',
        },
      ],
      address: {
        postCode: 'test',
        country: 'test',
      },
    },
    assetsList: [
      {
        description: [
          {
            language: 'En',
            text: 'Sports News Updated',
          },
        ],
        assetType: 'Description',
        assetLink: 'NewsLink',
        filePath: 'Not yet fill',
      },
    ],
  };

https://i.sstatic.net/zSkfO.png

I have created a Stackblitz showcasing the issue. Any help in resolving this error would be greatly appreciated!

Check out the StackBlitz

Answer №1

The assetsListDescriptionFormData cannot serve as a getter because you must specify the "index" of the assetsList -since it is a FormArray within another FormArray- or create a function that takes an index parameter.

getAssetsListDescriptionFormData(index:number) {
    return <FormArray>this.assetsListFormData.at(index).get('description');
}

To implement this, use the following:

<div formArrayName="description" style="position: relative;">
   <div *ngFor="let description of 
              getAssetsListDescriptionFormData(assetsListGroup).controls; 
              let assetsListDescriptionGroup=index">
        
      <div class="form-fields-wrapper" [formGroupName]="assetsListDescriptionGroup">
              ...
      </div>
   </div>
</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

Combine Two Values within Model for Dropdown Menu

I am currently facing a situation where I have a select box that displays a list of users fetched from the backend. The select box is currently linked to the name property of my ng model. However, each user object in the list also contains an email proper ...

The value is not being found in my form, and the slide-toggle is consistently checked

I am encountering an issue with my forms. On the web, all my slide-toggle options are pre-checked as shown in the image provided. I suspect that the problem lies within the patchFor(){} function. Could someone please review my code for me? I have attempte ...

Using ScaleTime on the X axis with d3 v4 in TypeScript for Angular2 on DefinitelyTyped

For my angular2 component, I am using the block provided in this link (a simple line chart): https://bl.ocks.org/d3noob/6f082f0e3b820b6bf68b78f2f7786084 I have successfully completed the following steps: npm install d3 --save npm install @types/d3 --save ...

Deleting a property once the page has finished loading

My issue is a bit tricky to describe, but essentially I have noticed a CSS attribute being added to my div tag that seems to come from a node module. The problem is, I can't seem to find where this attribute is coming from in my files. This attribute ...

We are currently unable to identify the specific web framework being used. For further details, please refer to the firebase-debug

I encountered an issue during the deployment process of my Angular application to Firebase Hosting using the firebase deploy command. The error message I received is as follows: [2024-01-31T22:26:15.400Z] --------------------------------------------------- ...

Tips for performing a cast when initializing a new object using TypeScript

Is there a way to create a new object using the map function on employees in TypeScript without declaring a variable? employees.map(x=> ({name: x.firstName})); This way, I can have VSCode intellisense while making my new object. However, if I try em ...

What is the best way to eliminate duplicate files from an Angular input file?

Currently, I am working on a web application that includes a feature to upload files. For each file, there is a separate input field. The issue arises when a user selects a file from the same input field twice, resulting in two different files of the same ...

Issue with ngClass not updating during Route Event

I am using a bottomNavigation component that changes its style to indicate which route we are currently on. Below is the template for the bottom-navigation: class="menu-icon" [ngClass]="{ 'active-item': buttonActivated.value == '/my-goa ...

Angular 8 form controls becoming unresponsive while the list is being loaded

Hello, I am currently experiencing an issue in Angular. I have a page with a basic Angular form and two independent components that load lists from an API (comments with 500+ records and posts with 500+ records). The Problem: While trying to enter values ...

What is the process for transferring an existing collection or data in Firestore to a subcollection?

My firestore database currently has the following structure with documents: root | |---transactions/... I am looking to transfer all transactions to a new subcollection as shown below: root | |---users/user/transactions/... Any suggestions on how I can a ...

Prevent Angular Material Autocomplete from automatically updating the input field

As part of my project, I am working on creating a country autocomplete input feature. To make the input more reusable, I am looking to turn it into a component using the ControlValueAccessor interface. The goal is to have the component accept a FormControl ...

Can I modify the cookie domain for NestJS SessionModule on a per-request basis?

I am currently using NestJS with SessionModule to handle user cookies successfully. However, I have a requirement to override the domain name for certain requests. I am uncertain about how to achieve this within NestJS, as the domain setting appears to b ...

Using StencilJS to Incorporate CSS/SASS Styles from node_modules

I'm currently facing a challenge in importing a CSS file containing variables from a node_modules package. Despite trying to replicate the process outlined in stencil.config.ts, the builds continue to end up in a different location than intended, leav ...

Angular's conditional statement for handling three different cases

Can someone explain how to create a switch case or if statement in Angular that accommodates 3 different options? if Boards.status==1 then "Approved" if Boards.status==2 then "Pending" if Boards.status==3 then "Rejected" I attempted this approach, but ...

What is the best way to make Angular Material Fields fill 100% of the container width?

Can you help me figure out how to adjust form fields to maximum width on a screen size less than 840px? I'm currently using Angular, Angular Materials and Flex-Layout. I'm having trouble with making this CSS code work properly. Can you explain ...

How to dynamically assign a value in a React datepicker component in a React application

Having troubles with react-datepicker? I encountered an issue where setting the value of react-datepicker from props caused it to either not show the value or display a 'wrong time format' error. Here is a snippet of the Datepicker code: this.sta ...

Detecting Changes in Angular Only Works Once when Dealing with File Input Fields

Issue arises with the file input field as it only allows uploading one file at a time, which needs to be modified. Uploading a single file works fine. However, upon attempting to upload multiple files, it appears that the "change" handler method is not tr ...

Exploring the canDeactivateFn syntax with Angular Documentation

As a first-year university student, I recently discovered that the canDeactivate() guard in Angular is deprecated, while the canDeactivateFn guard functions without any issues. Admittedly, navigating through official documentation is still new to me. From ...

Tips on sorting an array within a map function

During the iteration process, I am facing a challenge where I need to modify certain values based on specific conditions. Here is the current loop setup: response.result.forEach(item => { this.tableModel.push( new F ...

Issues with FormArrays containing nested FormGroups

In my reactive form, I am using a FormArray to store FormGroup elements. Despite researching and trying various approaches, I still cannot determine why it is not functioning correctly. I have combined two simplified examples based on documentation and on ...