Verify that the input is zero and then proceed to deactivate the submit button if it is indeed zero in AngularJS

app.controller('HelloController', function($scope) {
  console.log($scope.input1);
  console.log($scope.input2);

  if (($scope.op_option == 4) && ($scope.input2 == 0)) {
    myForm.$invalid;
  }
});
<form id="calc" method="post" action="/" ng-app="myApp" name="myForm" ng-controller="HelloController" novalidate>

  <div class="row">

    <div class="col-xs-2">

      <input type="number" class="form-control" name="op1" placeholder="Enter 1st number" ng-model="input1" required>
      <span style="color:red" ng-show="myForm.op1.$dirty && myForm.op1.$invalid">
<span ng-message="myForm.op1.$error.required">Number is required</span>
      </span>
    </div>
    <div class="col-xs-2">
      <select class="form-control" name="op_option" ng-model="oper">
        <option value="1">+</option>
        <option value="2">-</option>
        <option value="3">*</option>
        <option value="4">/</option>
      </select>
    </div>
    <div class="col-xs-2">
      <input type="number" class="form-control" name="op2" placeholder="Enter 2nd number" ng-model="input2" required>
      <span style="color:red" ng-show="myForm.op2.$dirty && myForm.op2.$invalid">
<span ng-message="myForm.op2.$error.required">Number is required</span>
      </span>
    </div>
  </div>
  <br>
  <div class="row">
    <div class="col-xs-12">
      <button type="submit" class="btn btn-success" ng-disabled="myForm.$invalid">Calculate</button>
    </div>
  </div>
</form>

The purpose of this code snippet is to verify if the selected operation is division (i.e., option 4) and if the second input value is 0. In such a case, we want to prevent dividing by zero by disabling the submit button. The issue encountered is that the browser console displays 'undefined' for $scope.input1. Assistance in resolving this matter would be greatly appreciated

Answer №1

Check out the following solution below, it should help resolve your issue.

// Insert your code here
angular.module('app', [])
  .controller('HelloController', function($scope) {});
<div class="row">
  <div class="col-xs-12">
    <button type="submit" class="btn btn-success" 
    ng-disabled="oper == 4 && input2 == 0">Calculate</button>
  </div>
</div>

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

I am interested in scraping a webpage, how should I go about it?

Similar Questions: How to create a web crawler? Top techniques for parsing HTML I've always been curious about accomplishing a task like this. Although I do not own or manage the website (), the information I am interested in is publicly acce ...

Showing C# File Content in JavaScript - A Step-by-Step Guide

Is there a way to showcase this File (c#) on a JavaScript site? return File(streams.First(), "application/octet-stream", Path.GetFileName(element.Path)); I am looking for something similar to this: <img id="myImg" src="_____&qu ...

The ReCaptcha and AJAX Integration

I am attempting to display the ReCaptcha image using its AJAX API. I have been following the instructions from this documentation and observing the behavior of this demo. Despite my efforts, I am still unable to create a functional fiddle. I have added jsf ...

React is choosing to re-render only one of the setState functions, neglecting the other

I'm currently in the process of developing a basic Tic Tac Toe game using React. The Objective: Each button in the game has its own state. When a button is clicked, it should change from ' ' to an 'X' and update the player's ...

Switch Background Image - The image failed to display

Here is the code snippet I am currently working with. It involves displaying a background-image when clicking on a link with the class 'home-link', and not showing an image if the link does not have this class. The issue at hand: The problem ari ...

The functionality of alpine.js x-for update is not functioning as intended

I have implemented a basic x-for loop on data from the Alpine Store (need it to be global). My objective is to modify a specific row after the table has been rendered by the x-for. Codepen: https://codepen.io/roniwashere/pen/oNMgGyy <div x-data> ...

Is there a way to design a form that appears as though it's levitating above a div element?

I am new to web development and currently practicing by converting PSD designs into HTML pages. I am facing an issue where I need to create a form that appears to be floating on top of a div, with the form being taller than the div itself. Here is an illu ...

Create a search feature based on names utilizing Node Express in conjunction with SQL database

After deciding to create an API with a search feature using SQL queries in node express, this is how I structured my code: app.get('/search/:query', (req, res) => { pool.getConnection((err, connection) => { if(err) throw err ...

Top method for preventing an HTTP 403 error when receiving the Set-Cookie header in order to establish the CSRF Cookie

As I interact with a REST API that includes CSRF protection measures, I am facing a common hurdle. Successfully obtaining the token and sending it back to the server seems to work smoothly. However, encountering an HTTP 403 error arises when initiating t ...

Utilize vue.js input field to search for a specific username within a constantly updating list of users

I have created an input search functionality to filter a list of users using PHP. Now, I am attempting to implement the same filtering feature in Vue.js so that when a user searches for a name, the filtered user will be displayed in the list. Below is the ...

What is the process by which Elastic Beanstalk executes Node.js application code?

While my application runs smoothly on localhost, encountering no issues, deploying it to other platforms such as AWS or MCS presents a challenge. Specifically, the problem lies in recognizing the file paths required by the consign library to load the route ...

Adding a C# variable to a URL in a Razor view using jQuery

My Razor page looks like this: @{ ViewData["Title"] = "mypage"; string ApiAddress = "https://localhost:8114/api/"; } <div> ... </div> @section Scripts{ <script> functio ...

Encountering a jQuery error while trying to utilize the $window.load

I have a code snippet that is functioning well when wrapped within a document ready event: jQuery(document).ready(function($) { $('tr[data-name="background_colour"] input.wp-color-picker').each(function() { //this section works fin ...

Repurposing React key usage

section, I am curious to know if it is standard practice to reuse a React key from one component to another. For instance, in the Row component, the key obtained from the Column component is reused for mapping the children of Row. const Table = props =& ...

Encountering an AngularJS 1.6 unit testing issue where the $scope is not defined, leading to a TypeError when trying to read the property 'setDescription' from undefined

While testing my angularjs 1.6.8 application with karma-jasmine, I keep encountering errors such as $scope is not defined and TypeError: Cannot read property 'setDescription' of undefined I have attempted to resolve these issues by searching onl ...

What are the steps to extracting JSON data in Node.js?

I am currently utilizing the API's node wrapper provided by MySportsFeeds. You can find more information about it here: https://github.com/MySportsFeeds/mysportsfeeds-node/blob/master/README.md The API call is functioning smoothly and the data is aut ...

A promise was caught with the following error: "Error in ./Search class Search - inline template:4:0 caused by: Maximum call stack size exceeded"

As a newcomer to Angular2, I am currently developing a web application that requires three separate calls to a REST API. To test these calls, I decided to simulate the API responses by creating three JSON files with the necessary data. However, my implemen ...

ngSwitchCase provider not found

While following a tutorial and trying to implement what I learned, I encountered an error that I'm having trouble understanding. The browser console shows an error message stating [ERROR ->]<span *ngSwitchCase="true">, but I can't figure ...

Refresh Angular 2 data after a related data update

I am facing an issue with my <select> elements in Angular. One for the Districts and another for the Cities. The districts are fetched using the getDistricts() function within the ngOnInit() function without any problems. However, I am unsure how to ...

Dealing with Errors in Express 4

After reviewing several posts, I am still confused about how to handle errors. Here is a snippet of my middleware: // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); err.status ...