区域应添加没有长度和呼吸输入值

时间:2016-02-15 12:25:20

标签: angularjs

在用户手动更改区域值时,结果必须相应地更改为手动编辑的区域值。这意味着如果我给出长度和呼吸然后只有区域正在计算但我需要区域应该在下一行打开时添加,并且区域必须手动进行,没有长度和宽度。



var app = angular.module('Calc', []);
               app.controller('Calc_Ctrl', function($scope) {

                    /* Start constants declaration*/
                    $scope.constant = {coeff : "0.003"};
                    /*End constants declaration*/

                    /*Start user input values and Function to add/remove input fields*/
                    $scope.choices = [{id : 'choice1', l2 : 10, b2 : 10}];
                    $scope.addNewChoice = function() {
                         var newItemNo = $scope.choices.length + 1;
                         $scope.choices.push({'id' : 'choice' + newItemNo});
                    };

                    $scope.removeChoice = function() {
                         var lastItem = $scope.choices.length - 1;
                         $scope.choices.splice(lastItem);
                    };

                    $scope.sum = function() {
                         var sum = 0;
                         angular.forEach($scope.choices, function(choice) {
                              sum += choice.l2 * choice.b2;
                         });
                         return sum;
                    }
                    /*End user input values and Function to add/remove input fields*/
               });

<!DOCTYPE html>
<html>
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src = "data.js"></script>
     <body>
          <div ng-app="Calc" ng-controller="Calc_Ctrl">
               <!--Start Input calculation-->
               <form  data-ng-repeat="choice in choices">  <br>  {{$index + 1}} :
                    Length:<input type="number" ng-model="choice.l2"  />ft
                    Breadth: <input type="number" ng-model="choice.b2"  />ft
                    Area: <input id="area" type="number" placeholder="Area" value="{{ choice.l2 * choice.b2}}" />sqft
                    <button  ng-click="addNewChoice()">+</button>
                    <button  ng-show="$last" ng-click="removeChoice()">-</button> </br>
               </form>
               <!--End Input  calculation-->
               <br>  <b>Output</b>
               <br> Area to be treated:  {{sum() + (sum() * 0.05)}}
               <p> chemical: {{(sum() + (sum() * 0.05)) * constant.coeff}}</p>
               <!--end Output calculation-->
          </div>
 </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

由于{{choice.l2 * choice.b2}}将其更改为{{choice.area}}并在数组中添加了一个区域元素,因此无法正常工作,但更重要的是(逻辑上)如果您希望您的区域为改变然后你必须更新你的长度和广度。也许你想要考虑一下 - 如果区域已被编辑,你是否允许更新长度和宽度?或者你是否应该允许首先编辑区域?。