Create a system for detecting changes in simple input elements and triggering a function to update the final result

There are a maximum of 12 inputs that represent the same entities or objects but with varying integer values. These values directly impact the final result displayed to the user. Whenever any of the input values change, a function needs to be triggered to update the result. Some developers recommend using hooks such as OnChanges() or DoCheck(), while others opt for @Input. However, in this scenario where there is no inheritance between inputs, using the @Input decorator would only add unnecessary complexity.... What would be the most suitable approach to implement this?

For example:

 <input type="number" [(ngModel)]="a.x">
 <input type="number" [(ngModel)]="a.y">
 <input type="number" [(ngModel)]="a.z">
 <input type="number" [(ngModel)]="b.x">
 <input type="number" [(ngModel)]="b.y">
 <input type="number" [(ngModel)]="b.z">

Result: {{result}}


updateResult() {
    result = a.x+a.y+a.z-(b.x+b.y+b.z)
}

Answer №1

What if we tried this instead?

 <input type="number" [(ngModel)]="a.x" (ngModelChange)="updateResult()">
 <input type="number" [(ngModel)]="a.y" (ngModelChange)="updateResult()">
 <input type="number" [(ngModel)]="a.z" (ngModelChange)="updateResult()">
 <input type="number" [(ngModel)]="b.x" (ngModelChange)="updateResult()">
 <input type="number" [(ngModel)]="b.y" (ngModelChange)="updateResult()">
 <input type="number" [(ngModel)]="b.z" (ngModelChange)="updateResult()">

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

Switching between Custom tooltips and default tooltips in Chart.js and Angular

I need to show a tooltip based on certain conditions options: { tooltips: if (tooltipCondition === true) { { mode: 'index', position: 'nearest' } } else { { enabled: false, custom: function (tooltipMo ...

Common problems encountered post Typescript compilation

I encountered the same problem. Below is my tsconfig settings: "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "newLine": "LF", &q ...

You cannot utilize Lesson as a JSX Component in Next JS TypeScript

Below is my updated page.tsx code: import Aspects from '@/components/Aspects'; import FreeForm from '@/components/FreeForm'; import Lesson from '@/components/Lesson'; import React from 'react'; import { Route, Route ...

Error: The class constructor [] must be called with the 'new' keyword when creating instances in a Vite project

I encountered an issue in my small Vue 3 project set up with Vite where I received the error message Class constructor XX cannot be invoked without 'new'. After researching online, it seems that this problem typically arises when a transpiled cla ...

What steps are involved in building a modal pop-up in Angular without using any pre-existing libraries

I am looking to create a modal popup when a button is clicked. I want to achieve this without relying on any additional dependencies. The method I have used below successfully creates a fully functional modal, but I am unsure if this is the best way to do ...

Angular rxjs subscription error method not functioning as intended

I have a component that is making an HTTP request to a Node.js backend. The component is using a service without leveraging ngrx. The issue I am facing is that when I receive an error response from the backend, the code within the error method of the subs ...

Error: The 'contains' property is not available for type 'never'

I'm facing a persistent error that is making my file display in red. I attempted to include types while using useRef(null), but the error continues to persist. Could it be possible that I am assigning incorrect types? const dropdownRef = useRef(null) ...

Getting object arguments from an npm script in a NodeJS and TypeScript environment can be achieved by following these steps

I'm trying to pass an object through an NPM script, like this: "update-user-roles": "ts-node user-roles.ts {PAID_USER: true, VIP: true}" My function is able to pick up the object, but it seems to be adding extra commas which is ...

The mat-datepicker is being displayed beneath the content

My Angular material design datepicker is displaying underneath the content. Within my module, I have a component that is rendered inside app-root. This component includes a mat-stepper with a mat-datepicker inside one of the steps. This is how the genera ...

Dynamically apply classes in Angular using ngClass

Help needed with setting a class dynamically. Any guidance is appreciated. Below is the class in my SCSS file: .form-validation.invalid { border: 2px solid red } In my ts file, there's a variable named isEmailValid. When this variable is set to ...

Tips for ensuring the HTML checkbox element is fully loaded before programmatically selecting it

When a user accesses the page, I want certain checkboxes to be automatically checked. To achieve this, I am storing the IDs of the HTML checkbox elements in a service. Upon entering the page, the service is utilized to retrieve an array containing these ID ...

Having trouble grasping this concept in Typescript? Simply use `{onNext}` to call `this._subscribe` method

After reading an article about observables, I came across some code that puzzled me. I am struggling to comprehend the following lines -> return this._subscribe({ onNext: onNext, onError: onError || (() => {}), ...

Utilize the click spinner feature on a single button in Angular

I have a table with a list of items and each item has a button to request a PDF document from the server. I want to show a spinner while waiting for the document to be received. Currently, when I click on a button, the spinner appears on all buttons instea ...

Can you provide guidance on defining functions using standard syntax and incorporating children in React with TypeScript?

There are multiple ways to type it, such as using the interface React.FC<YourInterface> or explicitly declaring in an interface the type of children as JSX.Element or React.Node. Currently, my approach is: const MyComponent: React.FC<MyInterface& ...

Guide to adding a CSS class to an Ionic2 toast

i found a helpful resource on applying cssClass to my toast component. within my HTML, I have buttons: <button ion-button (click)="presentToast()"> toast</button> Here is the code snippet from my .ts file: presentToast() { let toast = t ...

The getStaticProps() method in NextJS does not get invoked when there is a change in

I have integrated my front-end web app with Contentful CMS to retrieve information about various products. As part of my directory setup, the specific configuration is located at /pages/[category]/items/[id]. Within the /pages/[category] directory, you w ...

Error: Cannot access the 'people' property as it is undefined

Can anyone assist me with troubleshooting an issue I'm having while trying to set up a Google People API authentication service in Angular? I keep encountering the following error in the Chrome console: Uncaught (in promise): TypeError: Cannot read ...

In TypeScript, fetch JSON data from a URL and gain access to an array of JSON objects

I'm struggling to find a solution and implement it in the correct format. An API returns a JSON file to me via a URL. The format is as follows: {"success":true, "data":[ { "loadTimestamp":"2022-07-20T ...

Jhipster entity authentication

For my latest project with JHipster, I decided to incorporate Angular 2 and MongoDB. One of the entities I created is a 'doctor' with attributes such as name (string), login (string), password (string), and specialty (string). Now I want to enabl ...

Updating Key-Value pairs in an ArrayList using Angular 4

After importing json data into an arrayList and storing it in local-storage, the structure looks like this: [ { "id": 1, "name": "Albany", "manufacture": "Albany Superior Low Gi Sliced Brown Seed Bread 700g", "price": 1 ...