Deactivate and activate two buttons under the same condition (while entering information in forms) using Angular

Within my HTML form, there is a field labeled value. Alongside this field, there is an add button. When I click the add button, it duplicates the form field. My code is set up so that when I input a value (let's say 40) into the form field, then click the add button and input another value (like 50), a function calculates the sum of these values and compares it to the Totalvalue displayed above. If the sum exceeds the totalvalue, an error message appears.

Now, what I am trying to achieve is disabling the add button and enabling the submit button when the sum of the form values matches the totalvalue above. I attempted using [disabled]="isin" with a boolean to toggle between disable and enable. However, this approach did not work as intended (as pressing backspace after exceeding the total value kept the add button disabled).

Please provide feedback below if my question was unclear or needs further clarification.

You can view my project on StackBlitz here: https://stackblitz.com/edit/angular-ivy-fc8tcl?file=src%2Fapp%2Fapp.component.html

Answer №1

If we look at the SB example, the process of changing the value of this.isin doesn't happen again once it has already been set to true.

A simpler solution would involve replacing

if (this.total <= sum) {
  this.isin = !this.isin;
}

with

this.isin = this.total === sum;

Furthermore, update the [disabled] binding on the Add button to use the outcome from the checkTotal function, and utilize isin to bind to the Submit button, like so

<button ... [disabled]="!checkTotal()" ><span class="fa fa-plus"></span>Add</button>
...
<button type="submit" ... [disabled]="!isin">Submit</button>

Accessible SB link: https://stackblitz.com/edit/angular-ivy-mwywpt?file=src/app/app.component.ts

Another crucial point to mention: checkTotal() isn't triggered when a value is removed, so you must modify one of the input values to prompt the re-calculation of isin

Answer №2

You're certainly heading in the right direction, but in order to achieve that functionality, you'll need to listen for changes on the formControl's valueChanges event. This is where you can compare the current sum with your desired value and update the boolean values for the buttons accordingly.

https://angular.io/api/forms/AbstractControl#valueChanges

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

What is the best way to append an HTML element located by its ID to the body of a webpage

Is there a way to insert a DOM element with a specific ID into the body tag while clearing out all existing body nodes? The following approach doesn't seem to be effective: var elem = document.getElementById("email"); document.body.innerHTML = elem; ...

Determine the value from an object in the view by evaluating a string in dot notation

Seeking assistance with a recurring issue I've encountered lately. Imagine having two objects in AngularJS: $scope.fields = ['info.name', 'info.category', 'rate.health'] $scope.rows = [{ info: { name: "Apple", cate ...

What are the steps to accessing the scene, renderer, and camera objects in Forge Viewer v6 using TypeScript?

In the past, I could utilize code such as this: var container = viewer.canvas.parentElement; var renderer = viewer.impl.renderer(); var scene = viewer.impl.scene; To access Three.js objects in Forge Viewer. With version 6, how can I achieve the same usin ...

Guide on How to Navigate JSONP Responses in Goodreads

I've been working on a project that involves pulling data from Goodreads' API in order to display the rating for a specific book. However, I've hit a bit of a snag. Upon requesting data from this URL (https://www.goodreads.com/book/review_c ...

Determine the frequency of a specific string within an array by utilizing Lodash

I have retrieved an array of tags for blog posts from a database. Here is an example of the query result: ["apple","banana", "apple", "orange","grapes","mango","banana"]; I am looking to determine how many times each string is repeated within the array ...

Include the jquery library and embed a Google Map

Hello, I am using jQuery load to refresh my page every second. The problem arises when I add a Google Map to the page - the map appears and disappears every second. Is there a way to fix this issue? $(document).ready(function(){ <?php if( ...

Challenges in conducting asynchronous tests for Angular2 due to setTimeout complications

Using Angular2.0.1, I encountered an issue while trying to write unit tests for an angular component that involved async tasks. This is a common scenario and even the latest testing examples from Angular include async tests (see here). My test kept failin ...

Acquiring a fresh scope in Angular via a different component

In my project, I am developing an app using a component-based approach with Angular 1.5.5. As part of this development, I am utilizing d3js to create some elements with the class .floating-node. For each of these nodes, I am creating a new $scope and appe ...

Failure to successfully transmit data from Ajax to PHP script

When I click on a button in my HTML page, I am trying to retrieve information from my PHP code, but it is not connecting properly. The front page displayed in index.php is as follows: <!DOCTYPE html> <html> <head> <link rel="styleshe ...

Retrieve the native interface from the Puppeteer browser/page context

Can a native interface be obtained from the Browser or Page instance to verify if an object is an instanceof this interface? For example, in a testing scenario with jest (where CanvasRenderingContext2D is not available due to being a Node context rather t ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

Receiving Server Emissions in Vue/Vuex with Websockets

In my Vue component, I was using socket.io-client for WebSocket communication. Now that I've added Vuex to the project, I declared a Websocket like this: Vue.use(new VueSocketIO({ debug: true, connection: 'http://192.168.0.38:5000', })) ...

Smart method for repositioning multiple elements on the display

Imagine we have multiple divs displayed on a screen: https://i.stack.imgur.com/jCtOj.png ...and our goal is to move them collectively, either to the left: https://i.stack.imgur.com/KBfXC.png ...or to the right: https://i.stack.imgur.com/c1cUw.png An ...

Utilize the "use" tag as a fallback to png files

Having some trouble with displaying SVGs on older versions of IE. My main question is: Is there a way to switch to a png as a fallback while using the "use" tag? I'm currently working with Angular 2, so if there's a solution within this framewor ...

setTimeout in CSS animation causing issues

I recently created an animation using CSS3. The source code is quite simple. It involves displaying text and then making it disappear after 4 seconds. Below you can find the current source code: const intro1 = document.getElementsByClassName('intro ...

Choose FormGroup using Angular Directive

Can Angular reactive form controls be selected for a custom directive in a different way? Take a look at this code snippet: @Directive({ selector: '[formControl], [formControlName]', }) export class MyDirective { constructor( priv ...

Using Angular Platform-server (Universal) to route with query parameters

I've been struggling to describe a route with get parameters in my platform-server/universal application, but haven't had any luck so far. Does anyone have suggestions on how to achieve this? Based on my understanding of express routing, I atte ...

Exploring the power of Angular JS and Ionic by parsing data from a JSON

I'm currently developing an App with the IONIC framework and Angular JS, utilizing the Tinder cards feature recently introduced by Ionic (http://ionicframework.com/blog/tinder-for-x/). My goal is to read from a JSON file and use it as storage for my a ...

Issue with current time malfunctioning

For my latest project, I've been tasked with creating a karaoke application using javascript. The challenge is to synchronize the lyrics with the song as it plays. Here's a snippet of my HTML code: <div id="lyric"></div> <audio i ...

What are some methods for deactivating linked clicks while activating alternative links?

I am trying to prevent a link from being clickable after it has been clicked. Here is the code I have: <a class="cmd-7" href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a> <a class="cmd-14" href="? ...