Adjusting the binding of ngModel within a loop

Apologies, I am having difficulty articulating this issue.

I am attempting to display a form that iterates through an array of object keys and generates input fields based on the number of properties an object has.

For instance:

<form [ngFormModel]="dataForm" (ngSubmit)="save()">
    <fieldset>
        <legend>Data</legend>
        <div class="form-group" *ngFor="#key of dataKeys"
            <label>{{ key }}</label>
            <input 
                [(ngModel)]="data." + key
                class="form-control"
                ngControl="key"
                #key="ngForm">
        </div>
    </fieldset>
</form>

I am aiming for the output to resemble this:

<form [ngFormModel]="dataForm" (ngSubmit)="save()">
    <fieldset>
        <legend>Data</legend>
        <div class="form-group">
            <label>id</label>
            <input 
                [(ngModel)]="data.id"
                class="form-control"
                ngControl="id"
                #id="ngForm">
        </div>
        <div class="form-group">
            <label>score</label>
            <input 
                [(ngModel)]="data.score"
                class="form-control"
                ngControl="score"
                #score="ngForm">
        </div>
        ...
    </fieldset>
</form>

I understand that my current template structure is flawed, but I am struggling to find a viable solution. I thought about creating a custom directive, however, despite researching extensively, I am still unable to implement it successfully.

Thank you in advance for your assistance!

Answer №1

Here is the solution you're looking for:

[(ngModel)]="data[key]"

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

Angular - Automatically populate nested form with provided data

Here is the link to my StackBlitz project: https://stackblitz.com/edit/create-eez7wi?file=app/app.component.ts I am facing an issue where when I load the resources, it fills all fields except for the skill if more than 1 is entered. setResourceDTOS() { ...

Modify FrameColor of Material UI Inputs when Reset button is clicked

When using Angular Material UI in the Registermenu, I am facing an issue where clicking on the reset button deletes the content but leaves the red frames unchanged. Can anyone provide assistance with this problem? Thank you. Screenshot Here is the code f ...

Validation is a must in Angular 2

I am facing an issue with the default required validator in angular forms. My form has one input field and a submit button. There are two important files: example.form.ts example.form.template.html Here is my code setup: In the .ts file, I create ...

Angular (TypeScript) time format in the AM and PM style

Need help formatting time in 12-hour AM PM format for a subscription form. The Date and Time are crucial for scheduling purposes. How can I achieve the desired 12-hour AM PM time display? private weekday = ['Sunday', 'Monday', &apos ...

Steps for running an Angular application in IntelliJ:1. Open IntelliJ IDEA

I'm currently navigating through IntelliJ to set up Angular support. https://www.jetbrains.com/help/idea/2017.1/using-angular.html#install_angular_cli After successfully creating the project, I am unsure of how to run it. My guess is that I need to ...

Utilizing Hapi js as a proxy server for managing API requests

I am looking for guidance on setting up a proxy server using Hapi js to handle api calls. For example, if I send a request to www.example.com to retrieve data, instead of directly accessing www.example.com from my angular application, I want hapi js to a ...

What is the mechanism for invoking functions defined with the arrow syntax in Angular?

Referencing this code snippet from the tutorial at https://angular.io/tutorial/toh-pt4, specifically within the hero.component.ts file: getHeroes(): void { this.heroService.getHeroes() .subscribe(heroes => this.heroes = heroes); } After analyz ...

Choose the currently active md-tab within the md-dialog's md-tab-group

I need to create a dynamic md-dialog with an md-tab-group that has two tabs. The md-dialog should open based on the button clicked, displaying the corresponding tab content. The initial template where the md-dialog is triggered: <button md-button class ...

Exploring the implementation of if/else statements in Sass/HTML

I need assistance with customizing the appearance of a save button on my form. The button is currently disabled and appears in blue color, but I would like it to change to a different color until all fields within the form are completed. Even though it c ...

Unable to access property '_lastPathIndex' of an undefined value

In my component spec, I am simulating the Activated Route like this: class ActivatedRouteMock { public paramMap = of(convertToParamMap({ level: 'customer', id: '12345', })); } I have also added this class in the providers ...

Using the scrollIntoView method on an element with a height of 0 within an Angular 2+ application

I am currently working with the following HTML code snippet: <div class="row-20 st-margin" *ngIf="role == 'Administrator'" id="hr-data"> <div class="col-md-12"></div> </div> After ...

Disabling dates in Kendo Date Time Picker (Angular): An easy guide

<input id="startDate" kendo-date-time-picker k-ng-model="vm.startDate" k-on-change="vm.updateStartDate()" required /> Can someone please explain how to incorporate disabled dates into this date picker without utilizi ...

When using TypeScript, how can I effectively utilize the Component property obtained from connect()?

Software Development Environment TypeScript 2.8 React 16 Redux Foo.tsx interface IFooProps{ userId:number } class Foo extends Component<IFooProps>{ render(){ return <p>foo...</p> } } const mapStateToProps = (state: I ...

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 ...

Enable Parse5's case sensitivity

Recently, I've attempted to parse Angular Templates into AST using the powerful parse5 library. It seemed like the perfect solution, until I encountered an issue - while parsing HTML, it appears that the library transforms everything to lowercase. Fo ...

Is it necessary for me to individually download every x.d.ts file for my application?

Do I need to include the following line for type checking when using fs.readFile in TypeScript? /// <reference path="node.d.ts" /> Is it considered 'best practice' to download and maintain the most recent version of node.d.ts file in my ...

Remove the main project from the list of projects to be linted in

Currently in the process of transitioning my Angular application to NX and have successfully created some libraries. I am now looking to execute the nx affected command, such as nx affected:lint, but it is throwing an error: nx run Keira3:lint Oops! Somet ...

Anyone have any suggestions on how to resolve the issue with vertical tabs in material UI while using react.js?

I'm working on integrating a vertical tab using material UI in react.js, but I'm facing an issue where the tabs are not appearing. Here is the snippet of my code: Javascript: const [value, setValue] = useState(0); const handleChange1 = (event ...

Utilizing Node modules in TypeScript, Angular 2, and SystemJS: A Comprehensive Guide

In the process of developing a simple Angular 2 application, I am constructing a class named User. This class includes a function called validPassword() which utilizes the bcrypt library to validate user passwords: import { compareSync, genSaltSync, hashS ...

A single image path utilized for both development and production stages during the Angular build process

I am struggling to determine the correct path for my images to work seamlessly on both development and production environments. Currently, I can only get them to display properly on my local development server. However, when I use the command ng build --pr ...