检查输入是否为0并禁用提交按钮,如果为0 - Angularjs

时间:2015-09-26 21:40:26

标签: javascript 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>
&#13;
&#13;
&#13;

我希望这段代码检查op_option是否被分割(即4)并且input2是0.所以它将除以b 0.因此我想禁用提交按钮。 input2是第二个选项,op_option是操作,即在我的情况下除以禁用按钮。代码console.log($ scope.input1)在浏览器控制台页面上显示undefined。 请帮助实现这一目标

1 个答案:

答案 0 :(得分:0)

尝试以下选项。希望这能解决你的问题。

// Code goes 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>