2016-08-31 2 views
2

각도 js에 새로운 기능입니다. 각도를 사용하는 작은 앱을 만들고 있습니다.각도 값 범위를 업데이트하십시오.

<input type="text" ng-model="one" ng-change="sum()"> 
    <input type="text" ng-model="two"ng-change="sum()"> 
    <input type="text" ng-model="three"ng-change="sum()"> 
    <input type="text" ng-model="total" > 

컨트롤러 코드 :

$scope.sum = function(){ 
     $scope.total = parseInt($scope.one) + parseInt($scope.two) + parseInt($scope.three) 
    } 

모든 필드의 합이 완벽하게 작동이 내 코드입니다. 하지만 지금은 사용자가 전체 값을 변경 한 다음 다른 필드가 총 필드의 값에 따라 값을 설정할 때 수행하려고합니다.

+5

어떻게 u는 세 가지 중 하나가 당신이 당신의 총을 변경할 때 바뀌지해야하는 것을 가정 할 수있다 ??? – Ruhul

+0

'one = 1','two = 2','three = 3','total = 6'을 의미합니다. 'total'을 5로 변경하면 세 값 중 어느 값이 변경됩니까? '3'은 2가 될 수도 있고 2는 2가 될 수도 있고 1은 1이 될 수도 있습니다. 변경할 값을 어떻게 선택합니까? - 기본적으로 @Ruhul은 위의 설명에서 LOL을 말했습니다. – Jorrex

답변

0

당신이 이것을 기대합니까 ??

 #test { 
 
    background: white; 
 
    border: 1px double #DDD; 
 
    border-radius: 5px; 
 
    box-shadow: 0 0 5px #333; 
 
    color: #666; 
 
    outline: none; 
 
    height:25px; 
 
    width: 275px; 
 
    } 
 
<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <script data-require="[email protected]" data-semver="1.4.1" src="https://code.angularjs.org/1.4.1/angular.js"></script> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    
 
    </head> 
 

 
    <body> 
 
    <div ng-app="myApp" ng-controller="myCtrl"> 
 
     <table> 
 
    <tr><td><b>Value1</b></td><td> <input type="text" id="test" ng-model="one" ng-change="sum()" /></td></tr> 
 
      <tr></tr> 
 

 
     <tr><td><b>Value 2</b></td><td><input id="test" type="text" ng-model="two" ng-change="sum()" /></td></tr> 
 
     <tr></tr> 
 
    <tr><td><b>Value 3</b> </td><td><input id="test" type="text" ng-model="three" ng-change="sum()" /></td></tr> 
 
    <tr></tr> 
 
     <tr><td><b>Total</b> </td><td><input id="test" type="number" ng-model="total" ng-change="newsum()"/> </td></tr> 
 
     </table> 
 
     </div> 
 
    <script> 
 
var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.sum = function(){ 
 
     $scope.total = parseInt($scope.one) + parseInt($scope.two) + parseInt($scope.three) 
 
    } 
 
    $scope.newsum = function(){ 
 
    if($scope.total){ 
 
    var div=$scope.total/3; 
 
    var mod=$scope.total%3; 
 

 
    if(mod==0){ 
 
     $scope.one=div; 
 
     $scope.two=div; 
 
     $scope.three=div; 
 

 
    } 
 
    else{ 
 
\t var str=''+div; 
 
     var deci=parseInt(str.split(".")[0]); 
 
\t  
 
     $scope.one=deci; 
 
     $scope.two=deci; 
 
     $scope.three=deci+mod; 
 
    } 
 
    } 
 
\t else{ 
 
\t alert("Please Enter the number"); 
 
\t } 
 
\t } 
 
    
 
}); 
 
    </script> 
 
    </body> 
 

 
</html>

관련 문제