Learning to retrieve the value from a dynamically generated input tag in a TypeScript file

<div *ngFor="let task of arrayList">
<input id="task.fieldName" *ngIf="task.key === 'Others'" type="text" class="form-control">
</div>

When dealing with dynamically created input fields based on a condition, the challenge is accessing their values in the typescript file. One way to access an input field by name is using @ViewChild. However, I am curious if there are alternative methods to retrieve the value of dynamically created elements.

Answer №1

<div *ngFor="let task of arrayList">
  <input [(ngModel)]="values[task.key]" id="task.fieldName" *ngIf="task.key === 'Others'" type="text" class="form-control">
</div>
class MyComponent {
  values = {};
}

This approach ensures that the user-input values are stored in the values object like so:

{key1: 'abc', key2: 'def', ... }

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

Having trouble verifying a user's password in Postman using "bcrypt.compareSync" with Angular and the MEAN stack

I'm currently working on implementing user authentication before adding a JWT token, and I've encountered an issue where the 'if' check is generating this error: node_modules\bcryptjs\dist\bcrypt.js:265 th ...

Just completed the upgrade of my Angular project from version 9 to version 12, but now encountering issues with a module that utilizes Plotly

Here is the content of my app module file. All components and imports are in their respective places as specified in the documentation: import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from &apos ...

Using the record key as the index for the function argument type

In my current setup, I have : const useFormTransform = <T>( formValues: T, transform: Partial<Record<keyof T, (value: T[keyof T]) => any>>, ) => ... This is how it's used : type Line = { id?: string; fromQuantity: number } ...

Issue with Type-Error when accessing theme using StyledComponents and TypeScript in Material-UI(React)

I attempted to access the theme within one of my styled components like this: const ToolbarPlaceholder = styled('div')((theme: any) => ({ minHeight: theme.mixins.toolbar.minHeight, })); This information was found in the documentation: htt ...

The Angular2 Mvc5 demonstration application is experiencing compilation errors

I recently started using Visual Studio Enterprise 2015 Update 3 and decided to create a new project called "Angular2 Mvc5 sample application" from the templates available online. However, upon compiling the project, I encountered numerous errors such as: ...

Acquire data from promise using an intermediary service in Angular 5

I'm facing an issue with retrieving promise data through an intermediate service. The setup involves a component, an intermediate service, and an HTTP service. My component makes a call to the intermediate service, which then forwards the request to ...

Having trouble importing a file in TypeScript?

I needed to utilize a typescript function from another file, but I encountered an issue: I created a file called Module.ts with the following code snippet: export function CustomDirective(): ng.IDirective { var directive: ng.IDirective = <ng.IDire ...

How can I achieve the same functionality as C# LINQ's GroupBy in Typescript?

Currently, I am working with Angular using Typescript. My situation involves having an array of objects with multiple properties which have been grouped in the server-side code and a duplicate property has been set. The challenge arises when the user updat ...

What is the process for incorporating a new index signature into a class declaration from a file.d.ts in typescript?

I am facing an issue with a class in my project: // some npm module export class User { fname: string; lname: string; } Unfortunately, I cannot modify the declaration of this class from my project root since it is an npm module. I wish to add a new in ...

Waiting for the response from $http in Angular2

In almost all REST requests, I require the user's ID to be included. After logging in a user, I store the token in local storage and pass the ID to user.service.ts (using the function setUserId(userId);). However, when authenticating a user using onl ...

What is the best way to incorporate sturdy data types into the alternative function for this switch statement

const switchcase = (value, cases, defaultCase) => { const valueString = String(value); const result = Object.keys(cases).includes(valueString) ? cases[valueString] : defaultCase; return typeof result === 'function' ? result() : r ...

Why is the function not being called when declaring a variable, resulting in a type error?

click here reference: const fn1 = (arg1: { key: number, })=>{ console.log(arg1) } fn1({ key: 1 }) const data = { key: 1, a: 1, } fn1(data) fn1({ key: 1, a: 1, }) more info Assistance needed: Why is ...

Identifying changes in a property's value binding within Angular 4

One way to detect changes in a property decorated with @Input is by implementing the OnChanges interface: export class EmployeeComponent implements OnChanges { @Input() message: string; startDate: Date; ngOnChanges(changes: SimpleChanges) { fo ...

Unable to convert the value "Firefox" to the specified type 'Microsoft.VisualStudio.WebClient.Diagnostics.HtmlToolHost.PineZorro.DebugAdapterType'

I'm looking to switch from using Chrome to Firefox for my Angular project. I successfully installed the debug adapter from this link and it's working properly. However, when I attempted to replace launch.json in Vs2022, I encountered the followi ...

Mapping JSON objects to TypeScript Class Objects

I am in the process of transitioning my AngularJS application to Angular 6, and I'm encountering difficulties converting a JSON object into a TypeScript object list. In my Angular 6 application, I utilize this.http.get(Url) to retrieve data from an AP ...

Having issues with my Angular 4 + MVC web application not functioning properly in Internet Explorer

After creating an application using Angular 4 and MVC, I noticed a problem specifically with Internet Explorer. The application runs smoothly on Chrome, Firefox, and other browsers except for IE. Does anyone have any suggestions on how to resolve this br ...

Move the page to the beginning of the vertical stepper upon clicking the "next" button

I am currently working on optimizing a lengthy form to enhance user experience. To illustrate my point, I have come across a simplified example of the code I am dealing with which can be found here. My primary goal is to ensure that when a user proceeds t ...

Issue with data-* attributes in MaterialUI React component causing TypeScript error

I am encountering an issue while attempting to assign a data-testid attribute to a Material-UI Select component. The Typescript error I am facing is as follows: Type '{ "data-testid": string; }' is not compatible with type 'HTMLAttributes&a ...

Why is the imported package not being recognized in the namespace declaration of my Node.js TypeScript file?

Currently, I am utilizing the WebStorm IDE developed by JetBrains to modify a TypeScript file within a Node.js v8.6.0 project. The JavaScript version set for this project is JSX Harmony. In the beginning of the TypeScript source file, there is an import st ...

Error Message: Angular 5 with SignalR (DefinitelyTyped) - '$' Not Found in Typings

I am currently working on implementing SignalR into my Angular 5 application. To do this, I added the following TypeScript type definitions from DefinitelyTyped: npm install --save @types/jquery npm install --save @types/signalr The version of Typescrip ...