2016-02-12 6 views
0

BLOCKQUOTE После выбора категории, поле ввода длина, ширина, thikenes.based на этом входе я получаю этот выход: - Screed Кол-во: 1050, Цемент: 189 мешков, песок: 12.60cft Но проблема заключается в том, что после нажатия кнопки +, показывающей NaN, мне нужно сохранить предыдущую выходную машту, добавленную к новому выходу, установленному для значения NaN.После добавления нового поля на выходе приходит NaN в angularjs

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

 
    /* Start constants declaration*/ 
 
    $scope.type={"M20 (1:1.5:3)":{cement:"0.180",sand :"0.012",mmaggregate20:"0.017",mmaggregate12:"0.007"}, 
 
       "M21 (1:1.5:3)":{cement:"0.227",sand :"0.012",mmaggregate20:"0.017",mmaggregate12:"0.007"}}; 
 
         
 
    /*End constants declaration*/ 
 

 
    /*Start user input values and Function to add/remove input fields*/ 
 
    $scope.choices = [{id : 'choice1', l2 : 10, b2 : 10,d2: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*choice.d2; 
 
      }); 
 
      return sum; 
 
    } 
 
    /*End user input values and Function to add/remove input fields*/ 
 

 

 
    /*End function to select units*/ 
 

 
});
<!DOCTYPE html> 
 
<html> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <body> 
 
      <div ng-app="Calc" ng-controller="Calc_Ctrl"> 
 
       <script src="scc.js"></script> 
 
       <!--Start Input calculation--> 
 
       Select a category: 
 
       <select ng-model="selectedgrade" ng-options="x for (x, y) in type" > 
 
       </select> 
 
      
 
       <br> <b>Input</b> 
 
       <form data-ng-repeat="choice in choices"> 
 
        {{$index + 1}} : 
 
        Length:<input type="number" ng-model="choice.l2" /> 
 
        width: <input type="number" ng-model="choice.b2" /> 
 
        Thickness: <input id="area" type="number" ng-model="choice.d2" /> 
 
        <button ng-click="addNewChoice()">+</button> 
 

 
        <button ng-show="$last" ng-click="removeChoice()">-</button> 
 
        </br> 
 
       </form> 
 
       <h2>cement: {{selectedgrade.cement}}</h2> 
 
       <!--End Input calculation--> 
 
       <br> <b>Output</b><br> 
 
       
 
       Screed qty:{{(sum() + (sum() * 0.05))}}<br> 
 
       Cement:{{((sum() + (sum() * 0.05))*selectedgrade.cement)|number:0}}bags<br> 
 
       sand:{{((sum() + (sum() * 0.05))*selectedgrade.sand)|number:2}}cft<br> 
 
       
 
       <!--end Output calculation--> 
 
      </div> 
 
    </body> 
 
</html>

ответ

0

При добавлении нового пустого элемента, вы должны указать свойства l2, b2, d2 к нулю или изменение суммы кода функции на что-то вроде этого (потому что номер с неопределенным пойти NaN):

$scope.sum = function() { 
      var sum = 0; 
      angular.forEach($scope.choices, function(choice) { 
       if(choice && choice.l2 && choice.b2 && choice.d2) 
        sum += choice.l2 * choice.b2*choice.d2; 
      }); 
      return sum; 
    } 

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

 
    /* Start constants declaration*/ 
 
    $scope.type={"M20 (1:1.5:3)":{cement:"0.180",sand :"0.012",mmaggregate20:"0.017",mmaggregate12:"0.007"}, 
 
       "M21 (1:1.5:3)":{cement:"0.227",sand :"0.012",mmaggregate20:"0.017",mmaggregate12:"0.007"}}; 
 
         
 
    /*End constants declaration*/ 
 

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

 
    $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) { 
 
      if(choice && choice.l2 && choice.b2 && choice.d2) 
 
       sum += choice.l2 * choice.b2*choice.d2; 
 
      }); 
 
      return sum; 
 
    } 
 
    /*End user input values and Function to add/remove input fields*/ 
 

 

 
    /*End function to select units*/ 
 

 
});
<!DOCTYPE html> 
 
<html> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <body> 
 
      <div ng-app="Calc" ng-controller="Calc_Ctrl"> 
 
       <script src="scc.js"></script> 
 
       <!--Start Input calculation--> 
 
       Select a category: 
 
       <select ng-model="selectedgrade" ng-options="x for (x, y) in type" > 
 
       </select> 
 
      
 
       <br> <b>Input</b> 
 
       <form data-ng-repeat="choice in choices"> 
 
        {{$index + 1}} : 
 
        Length:<input type="number" ng-model="choice.l2" /> 
 
        width: <input type="number" ng-model="choice.b2" /> 
 
        Thickness: <input id="area" type="number" ng-model="choice.d2" /> 
 
        <button ng-click="addNewChoice()">+</button> 
 

 
        <button ng-show="$last" ng-click="removeChoice()">-</button> 
 
        </br> 
 
       </form> 
 
       <h2>cement: {{selectedgrade.cement}}</h2> 
 
       <!--End Input calculation--> 
 
       <br> <b>Output</b><br> 
 
       
 
       Screed qty:{{(sum() + (sum() * 0.05))}}<br> 
 
       Cement:{{((sum() + (sum() * 0.05))*selectedgrade.cement)|number:0}}bags<br> 
 
       sand:{{((sum() + (sum() * 0.05))*selectedgrade.sand)|number:2}}cft<br> 
 
       
 
       <!--end Output calculation--> 
 
      </div> 
 
    </body> 
 
</html>

Смежные вопросы