2016-09-30 37 views
0

值這是我在角JS應用:復位後,角取回先前輸入

http://cmpe-273-010825867-fall-2016.herokuapp.com/

的問題是,當通過點擊數字鍵I輸入值到兩個文本,他們進入。當我重置時,這些值消失。 但是,當我再次使用數字按鈕重新輸入數值時,這些值會被重新添加到復位前的值中。

這裏是我的角度代碼:

var myApp = angular.module('myApp', []); 

myApp.controller('myController', function($scope) { 


    function resetFields(){ 
     $scope.opReset=' '; 
    } 

     function add(x, y) { 
     return x + y; 
     } 

     function sub(x, y) { 
     return x - y; 
     } 

     function mul(x, y) { 
     return x * y; 
     } 

     function div(x, y) { 
     return x/y; 
     } 

     function calc(op, x, y) { 
     return $scope.operators[op](parseInt(x, 10), parseInt(y)); 
     } 

     $scope.operators = { 
     '+': add, 
     '-': sub, 
     '*': mul, 
     '/': div 
     }; 
     $scope.op = '+'; 
     $scope.calc = calc; 

}); 

我的HTML:

<div ng-app="myApp"> 

       <div ng-controller="myController" ng-init='isFocused=true'> 

<form> 

            <div class="col-lg-5 col-md-8 col-sm-12 col-xs-12"> 
             <input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' value="{{opReset}}" autofocus> 
            </div> 


            <div class="col-lg-2 col-md-8 col-sm-12 col-xs-12 text-center" style="font-size:32px"> 
             {{op}} 
            </div> 


            <div class="col-lg-5 col-md-8 col-sm-12 col-xs-12"> 
             <input width="100%" type="text" class="form-control" style="height:50px; font-size:32px" ng-model="operand2" ng-focus=' isFocused="operand2" ' id="operand2" value="{{opReset}}"> 
            </div> 

            <script src="js/custom.js"></script> 

           </div> 

          </div> 


          <div class="col-lg-5"> 

           <div class="row"> 

             <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 eqProps text-center"> 
              = 
             </div> 


             <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12"style="font-size:32px"> 

              {{output}} 

             </div> 

           </div> 

          </div> 

        </div> 



        <div class="row"> 


         <div class="col-lg-1"></div> 

         <div class="col-lg-7"> 

          <div class="row"> 

            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1"></div> 
              <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10"> 
               <div class="row-fluid"> 

                <button class="btn btn-danger btn-fonts" type="reset" value="reset" ng-click=" opReset=resetFields() ">C</button> 
                <div class="btn btn-default btn-fonts" ng-repeat="n in [0]" style="margin-left:2px" ng-click='$parent[isFocused]=$parent[isFocused]+""+n' ng-disabled='isFocused===true'>{{n}}</div> 
                <div class="btn btn-default btn-fonts" ng-click="output=calc(op,operand1,operand2)" style="visibility:hidden">=</div> 
                <div class="btn btn-primary btn-fonts" ng-click="output=calc(op,operand1,operand2)">=</div> 

        </form> 

回答

-1

請爲NG-模型值空值(操作數,操作數)

+0

http://jsfiddle.net/nzPJD/ –

1

爲什麼你使用value =「operand1」呢?

<input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' value="{{opReset}}" autofocus> 

您可以直接使用NG-模型來初始化值,以及明確它點擊Ç按鈕。

更改您的文本區號以刪除values

<input width="100%" type="text" class="form-control" id="operand1" style="height:50px; font-size:32px" ng-model="operand1" ng-focus=' isFocused="operand1" ' autofocus> 

然後在resetFields功能,採用改變數值:

$scope.operand1=''; 
$scope.operand2='';