2015-09-26 75 views
0

檢查輸入爲0,禁用提交按鈕,如果0 - 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"> 
 
\t \t \t \t \t \t <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"> 
 
\t \t \t \t \t \t <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>

我想這個代碼,以檢查是否op_option是鴻溝(即4)和輸入2爲0。因此,這將劃分b 0.因此我想禁用提交按鈕。 input2是第二個選項,op_option是操作,即在我的情況下,禁用按鈕。此外代碼console.log($ scope.input1)在瀏覽器控制檯頁面上顯示爲未定義。 請幫助做到這一點

+0

您可以使用表單上的提交監聽器來檢查選擇的值和輸入,如果不合適,防止提交併告知錯誤的用戶。 – RobG

回答

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>

相關問題