Tips for binding data to numerous dynamic controls

Implementing reactive forms in my Angular project allowed me to create a simple form for adding employee work hours and breaks. The challenge I encountered was the inability to bind data from break controls.

In the .ts file

export class AddAppointmentFormComponent implements OnInit{
  addAppointmentForm!: FormGroup
  constructor(private _fb:FormBuilder, private _wfmService:WFMService) {}
 ngOnInit(): void {
    this.addAppointmentForm = this._fb.group({
      typeOfDayId :['',Validators.required],
      appointMentDate: ['', Validators.required],
      from: ['', Validators.required],
      to: ['', Validators.required],
      breaks: this._fb.array([
      ]),

    })
  }

  get breaks(){
    return this.addAppointmentForm.get('breaks') as FormArray;
  }

  addBreak(){
    this.breaks.push(this._fb.group({
      from: ['', Validators.required],
      to: ['', Validators.required],
    }))
  }

  removeBreak(i:any){
    this.breaks.removeAt(i);
  }
}

In the .html file

<div mat-dialog-content [align]="'start'">
  <h4 mat-dialog-title style="text-align: center;">Add New Appointment</h4>
  {{ addAppointmentForm.value | json }}
  <form [formGroup]="addAppointmentForm">

    <!-- HTML code skipped for brevity -->

  </form>

  <div class="btn__actions">
    <button class="btn btn-danger" mat-dialog-close>Close</button>
    <button style="margin-left:10px ;" class="btn btn-primary">Create</button>

  </div>


</div>

The desired output is:

{{ addAppointmentForm.value | json }}
"breaks": [ { "from": "10:00", "to": "10:30" }, { "from": "14:00", "to": "14:30" } ] }

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

Answer №1

The bid you are placing is spanning between two different formControlNames: [formControlName]="i".

It is recommended to create a div or ng-container with [formControlName]="n", and include two child div elements with

[formControlName]="'from'"
and
[formControlName]="'to'"

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

When attempting to incorporate a third-party library into Angular2 using the CLI, the process did not go as smoothly as anticipated

Trying to integrate a third party JavaScript library sockJS into my Angular2 project... system.config.ts: /** Mapping relative paths to URLs. */ const map: any = { 'sockjs-client': 'vendor/sockjs-client/' }; /** Configuration for use ...

How can I design unique navigation menus using HTML, CSS, JavaScript, and jQuery?

Is there a way to create menus where clicking on a holiday/seasonal category opens up all related lists automatically? For example: https://i.sstatic.net/Nctd1.png I've been searching online for submenu options but haven't found the right metho ...

Having trouble with the collapse feature on my DataList cards

I have a sample card and a DataList on my webpage. The collapse functionality is functioning correctly on the example card, but I am unable to get it to work on the actual cards. Below is the code snippet for reference: <div class="resultadosEspe ...

Guide to aligning form data with Bootstrap

Struggling to align my form data in the center below. I have attempted various online solutions without success. The use of bootstrap and offset is causing issues with the title for the form and the actual form itself. Any suggestions would be highly appre ...

Encountered an issue while trying to install the package '@angular/cli'

Encountered errors while attempting to install @angular/cli using npm install -g @angular/cli. The node and npm versions on my system are as follows: C:\WINDOWS\system32>node -v v 12.4.0 C:\WINDOWS\system32>npm -v 'C ...

Encountered CORS error when attempting to access the dynamic menu API after logging

Currently, I am working on an Angular 6 and Codeigniter project. In this project, the slider and navigation menu bar are being fetched dynamically through a REST API. Everything runs smoothly until the login process, where a CORS error is encountered. htt ...

Automate populating input fields with data

I need help with a form that has four input boxes. Is it possible to automatically fill the remaining three input boxes with the value entered in the first box when the user clicks a button using JavaScript? Or should I aim to prefill the textboxes with ...

What steps can be taken to disable auto correction in ngx date picker?

In my application, I am utilizing ngx-datepicker with 'DD.MM.YYYY' as the dateInputFormat in the configuration settings of the date picker. The challenge arises when I manually input a date following the format 'YYYY.MM.DD', as the ente ...

Tips on creating tabs in angular7 that toggle between two distinct components

In my Angular 7 project, I have created three different components: ng g c mycomp1 ng g c mycomp2 ng g c mycomp3 Now, I am looking to build a tab within the mycomp1 component that will have the following layout: https://i.sstatic.net/LZdyM.png When the ...

Do I need to use [providers] when implementing constructor dependency injection?

Currently following a step-by-step guide on managing tasks and it includes the following code snippet: import {TodoDataService} from './todo-data.service'; @Component({ // ... providers: [TodoDataService] }) constructor(private todoDa ...

Issues with the functionality of the accordion menu

I have implemented an accordion menu on my website. However, I am facing an issue where the 2nd menu always slides up when it gets loaded instead of sliding up after clicking on it. I want the behavior to change so that the sub menus elaborate only upon cl ...

Connecting CSS and JS files in JSP was a bit of a challenge

Just starting out with jsp and using Glassfish server via netbeans. Struggling to link my jsp file with css and javascripts. Below is the structure of my files: Web Pages --Web-Inf --assets --css --style.css --js --jquery.js ...

Ways to retrieve data from response instead of subscription JSON in Angular 2/4

My Service : retrieveData(url,request) { return this.http.post(this.apiUrl+url,request).subscribe( (response) => {return response.json()} ); } My Component : ngOnInit() { this.data = this.dataService.retrieveData(&apos ...

Is there a way to create a marker that changes color when hovering over the text directly below it?

Hi everyone, I hope you're all doing well. Currently, if you hover over the marker, it changes color. However, I want this effect to apply when hovering over the text below as well. Is there a way to create a connection between these two div elements? ...

Identify and mark labels when hovering over them

Hello everyone! I'm completely new to this, so please be gentle :). Apologies in advance if my terminology is off. I have zero experience with HTML, but I landed here from working with FileMaker. Currently, I am developing a FileMaker database for my ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Using Chrome or Firefox's inspector tool to make changes to HTML code

Is it possible to save changes made to HTML elements in Chrome/Firefox inspector directly to a local file on my desktop? ...

What strategies can I use to steer clear of the pyramid of doom when using chains in fp-ts?

There are times when I encounter a scenario where I must perform multiple operations in sequence. If each operation relies solely on data from the previous step, then it's simple with something like pipe(startingData, TE.chain(op1), TE.chain(op2), TE. ...

Having trouble with JQuery's append method on the <head> tag?

I am having an issue with this particular code block. It seems to be unable to insert the meta tag into the head section. Any suggestions on how to resolve this problem? <html> <head> <title>hello world</title> </head> < ...

A guide to implementing vue-i18n in Vue class components

Take a look at this code snippet: import Vue from 'vue' import Component from 'vue-class-component' @Component export default class SomeComponent extends Vue { public someText = this.$t('some.key') } An error is being thr ...