Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this:

<input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">
, and display the result in another textbox using ngModel below.

<input matInput type="text" [(ngModel)]="calculatedResult" formControlName="avgCapacity">.

The expected output: When a value is entered in Textbox0, the calculateBCT function should be called and the result should reflect in Result0 textbox only. The same goes for Textbox1 calling calculateBCT and Result1 being updated accordingly, and so on.

If anyone can help me achieve this expected output, it would be greatly appreciated.

Here is a demo on StackBlitz with the code provided

Answer №1

Give this a try and see if it makes a difference.

To make changes in app.component.html, follow these steps:

  • Remove the line
    [(ngModel)]="calculatedResult"
    since you are already using formControlName
  • Pass index i to calculateBCT by adding
    (keyup)="calculateBCT($event, i)"

In app.component.ts, update the calculateBCT method as shown below:

    calculateBCT($event, index: number) {
      // Your current logic remains unchanged...
      const calculatedResult = result ? result : 0;
      // Make sure to add checks to confirm that formControl exists
      const formControl = this.itemTypes().at(index).get('avgCapacity');
      formControl.setValue(calculatedResult);
    }

Note:

If you have formGroup in your html file, simply pass lineItem as the second parameter to calculateBCT and access formControl like so:

const formControl = lineItem.get('avgCapacity');

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

Toggle the input box by clicking the button

How do I show or hide the input box (blue square) when I click the search button (red square)? Link Image I attempted to create the transition in CSS and also experimented with JavaScript, but the JavaScript part confused me. Here is what I tried: $(" ...

Select a user at random from the reactions in the message

Is there a way to select a user at random from message reactions on Discord? Despite going through all the documentation, I'm still unsure about how to do this. ...

Struggling with replacing text in an array using Javascript (Angular)

I am facing an issue where I need to remove the 'hello' substring from each object field in my objects array. However, I keep getting an error message saying "Cannot read property 'indexOf' of null". This error is occurring because I am ...

File or directory does not exist: ENOENT error occurred while trying to scan 'pages'

Having trouble adding a sitemap to my nextjs app - when I go to http://localhost:3000/sitemap.xml, I get this error: Error: ENOENT: no such file or directory, scandir 'pages' https://i.sstatic.net/cxuvB.png The code in pages/sitemap.xml.js is a ...

Struggling to spot my error (codewars, javascript, level 8)

Can You Translate?! After receiving a message on WhatsApp from an unfamiliar number, you wonder if it's from the person with a foreign accent you met last night. Your task is to write a simple function that checks for various translations of the word ...

Maximizing the power of Webpack alongside Google Maps API

I have been using Webpack along with the html-webpack-plugin to compile all my static files. However, I am facing an issue when integrating it with the Google Maps API. Here is the code snippet: var map; function initMap() { map = new google.maps.Map(d ...

Execute a function once an observable variable has been successfully initialized

I'm currently putting together a chat application using socket.io in Angular. I've encountered an issue where I can't seem to execute a particular code or function right after an observable variable is initialized through subscription. The i ...

Attempting to alter an image with a click and then revert it back to its original state

I'm currently working on a feature that toggles an image when a specific class is clicked. The code I have so far successfully switches from 'plus.png' to 'minus.png' upon clicking, but I need it to switch back to 'plus.png&ap ...

Issue with MomentJS inBetween function consistently displaying incorrect results

I'm currently working on Vue Datatable and I have a specific requirement to search for records between two dates. I am using the moment.js library and the inBetween function to achieve this. Strangely, when I hardcode the dates, the function returns t ...

How should the Facebook avatar be displayed using AngularFire in a correct manner?

I am working on an app where I want to display the avatar of the logged-in Facebook user, but I'm struggling to find a clean solution. To show the user's avatar, you need to have their user_id and accessToken. While user_id can be obtained using ...

Is it possible for a table row's cell to determine the value of the cell in the row above it?

Let me illustrate my issue with an example. Consider the following table: <table> <tr> <th>First</th><th>Second</th><th>Third</th> </tr> <tr> <td>A</td><t ...

Upgrading from Angular 4 to 9

What is the recommended approach for migrating from Angular 4 to 9 - should one go directly from 4 to 9, or is it better to first upgrade to version 7 and then to 9? ...

Elements of Data Pagination in Vuetify Data Tables

My data-table is filled with thousands of data inputs, so I am using the default Vuetify pagination to display only 5, 10, or 25 items at a time on the table. However, I am in need of a way to determine which data is currently visible on the table. For ex ...

Tips for creating Junit tests for a CDK environment

As a newcomer to CDK, I have the requirement to set up SQS infrastructure during deployment. The following code snippet is working fine in the environment: export class TestStage extends cdk.Stage { constructor(scope: cdk.Construct, id: string, props: ...

The functionality of Angular Custom Validation using Directive is experiencing issues within the ng-change event

I have created a custom validator using directives to validate email addresses. The validation is triggered by a method called on ng-change, but the validation only displays on the second change of the input. <textarea id="txtEmail" name=&qu ...

On mobile devices, the height of the absolutely positioned element is larger than its

I currently have an element with a background image that includes an absolutely positioned element at the bottom. As the screen width decreases, the height of the absolutely positioned element increases and surpasses the parent element. Since absolutely p ...

"Discrepancy found in results between Node-Fetch POST and Firefox POST requests

I have encountered a challenge while trying to replicate a successful log-in process on a website using node-fetch after being able to do so with Firefox. The login process consists of three stages: Visiting /login which returns a sessionToken from the we ...

What is the best way to merge various scope models in Angular?

I am currently working with the following variables: $scope.formTitle = ''; $scope.formDesc = ''; $scope.fields = []; However, I would like to merge these into a single object named $scope.theForm for easier conversion to ...

What is the best way to spin a div HTML layer?

I'm trying to modify a Div layer that looks like this: ... <style type="text/css"> <!-- #newImg { position:absolute; left:180px; top:99px; width:704px; height:387px; z-index:1; background-image:url(../Pictures/rep ...

Struggling to make dynamically created SVG 'use' elements function properly

SVG offers a unique system with symbol and use where icons can be defined once and reused throughout the SVG using use. However, I am having trouble getting it to work from JavaScript. You can view an example on this JSFiddle link. When programmatically ...