What is the method to retrieve the control name of a dynamic Angular field?

I am currently working on a feature where I need to dynamically add new controls to my formBuilder.

/* templatesTypes = ["string1", "string2", "string3","string4"] */
/* templates = [{templateType: "string1", ...}, {templateType: "string2"}, ...]*/

this.agency = this.fb.group({
  name: [],
  templates: this.fb.array([])
});

const arrayControl = this.agency.controls['templates'] as FormArray;
const objControl = {};
templatesTypes.forEach(templateType => {
  objControl[templateType] = [{
    value: templates.filter(template => template.templateType === templateType)
  }];
  arrayControl.push(this.fb.group(objControl));
});
<div formArrayName="templates" *ngFor="let templateType of agency.get('templates').controls; let i = index">
  <div [formGroupName]="i">
    <label>{{ templateType.name }}</label>
    <ng-select  
      [items]="agency.get('templates').controls[i]" 
      bindLabel="templateName" 
      formControlName="{{ templateType.name }}">
    </ng-select>
  </div>
</div>

The naming of the control is based on the dynamic template name ("string1", "string2", ...).

However, I am facing challenges in retrieving the control name dynamically. I have tried using templateType.name, but it does not seem to return anything.

Answer â„–1

The templateType within the HTML template is an instance of AbstractControl, which does not contain a name property.

I believe this piece of code should function correctly.

<div formArrayName="templates" *ngFor="let templateType of agency.get('templates').controls; let i = index">
  <div [formGroupName]="i">
    <label>{{ templateType.name }}</label>
    <ng-select  
      [items]="agency.get('templates').controls[i]" 
      bindLabel="templateName" 
      formControlName="{{ templates[i].templateType }}">
    </ng-select>
  </div>
</div>

Reference: - AbstractControl: https://angular.io/api/forms/AbstractControl

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

The timing of jQuery's .load function appears to be off, catching us by surprise

My current challenge involves loading returned html from an .aspx page through AJAX, but facing a timing issue with a click event that needs to occur before executing some essential tasks. Specifically, the process begins when a user types in a text field ...

Adjust the appearance of matSelect when the selection menu is activated

What is the best way to adjust mat-select properties when its options are open? <mat-select class="selector"> <mat-option><mat-option> </mat-select> .selector:focus { color: green; } I attempted using focus, but ...

Using Javascript to trigger an event when an option is changed in an ASP dropdownlist

Can someone provide me with sample javascript code that can be used to determine if an item has been selected in a dropdown list and then make it visible? ...

I'm having trouble getting this angular expression to cooperate in Plunker. Can anyone shed some light on why {{ 843 / 42

I'm currently diving into Angular with the help of Plural Sight. The initial lesson dives into utilizing the ng-app directive. For those interested, here's a direct link to the Plunker editor: http://plnkr.co/edit/HIDCS8A9CR1jnAIDR0Zb?p=preview ...

Issue with change detection in Angular array authentication

I am currently working with a parent / child relationship consisting of two components: "Properties" - the parent, and "listingsList" - the child. Below is the parent template: <div class="row"> <div class="col-9 p-2"> ...

Is there ever a situation when it's acceptable to forego graceful degradation?

Currently, I am constructing a video-sharing CMS that heavily relies on jQuery and ajax for various functionalities such as rich UI effects and data submission/retrieval from the database. Unfortunately, if JavaScript is disabled, about 90% of the function ...

When an array object is modified in Vue, it will automatically trigger the get method to validate any

One of the challenges I am facing is related to a button component that has a specific structure: <template> <button class="o-chip border-radius" :class="{ 'background-color-blue': theValue.isSelected, ...

What is the proper way to expand a TypeScript class?

I'm facing a dilemma with the code snippet below. The line m.removeChild(m.childNodes[0]) is causing an issue with the TypeScript compiler. I'm unsure if childNodes: BaseNode[]; is the correct approach in this scenario. class BaseNode { childNo ...

Ways to extract a Bearer Token from an Authorization Header using JavaScript (Angular 2/4)

When working with JavaScript, I have successfully implemented a method for authenticating to my server using an http post request. Upon receiving a response from the server, it includes a JWT in an Authorization header like this: Authorization: Bearer my ...

After transforming an angular project into an npm module, where should the html, css, and other files be placed? Additionally, what is the process for converting a project into

I am looking to modify the ngx-material-timepicker node module by making changes to the basic HTML and CSS of the project. However, I have found that there are no HTML or CSS files available in the node_modules-> ngx-material-timepicker folder, only TS ...

React's useState function does not update the FileReader

I've been working on extracting values from an XML file using the code below. However, I'm encountering an issue where the console always outputs 'undefined'. Can anyone help me identify where I might be going wrong? This is my JavaSc ...

Increased impact of dynamically added elements following page transition

After implementing dynamic functionality from jQuery in my code, I encountered an issue where if I navigate back one page and then return to the original page containing the added elements, they seem to trigger twice upon clicking. To troubleshoot, I incl ...

What to do when calling disabled() on a FormControlName causes all form fields to become invalid?

While working with a reactive form, I have observed that setting a formControlName to disabled() can cause the entire form to become invalid. Is there a way to ensure the form remains valid even after disabling a control? console.log('Before:' ...

`Why won't Puppeteer let me pass a variable into a page URL parameter?`

Encountered an error message: Error: Protocol error (Page.navigate): Invalid parameters Failed to deserialize params.url - BINDINGS: mandatory field missing at position 49... The issue arises when trying to pass a variable into the page URL parameter as s ...

The element's position remains unchanged after the .click re-event in the function

Welcome to my first attempt at using jQuery! Please bear with me as I navigate through this learning process. Here's the challenge: I have a series of elements in my HTML. <div class="garden"> <div class="point left">&#9668;</d ...

Is the presence of an excessive number of arguments in the object that includes functions an instance

In my program, I have implemented a feature where the user can provide an array to determine which functions are executed in a loop. However, managing the list of variables that need to be passed into each function has become challenging as the list keeps ...

The JavaScript Date() string that was inserted into the database is now elegantly displayed

Can you help me figure out how to format a date string that's stored in a database like this: 2016-09-13T18:18:08.518Z, into a more visually appealing format such as: 13 September 2016? Thank you in advance! ...

What is the most effective way to provide an optional parameter to each child during the rendering of a component

The parent component contains a non-optional prop that I need to pass to each child without explicitly defining it in every child component. interface Props { foo: true; } export class Parent extends React.Component<Props>{ public render() ...

Utilizing keyboard shortcuts for webpage navigation (within a content script)

Just to clarify, I'm not looking for code assistance right now. I mostly need a starting point. My goal is to create a Chrome browser extension that enables me to use keyboard keys for navigation on a specific webpage. The page in question belongs t ...

The AJAX post request contains no data (an empty string in the database)

I am facing an issue with my AJAX code. $(document).ready(function() { var activeTD; $(".td_test").click(function() { $("#bModal").modal('show'); activeTD = this; return false; }); $("#ajaxtest").submit( ...