1
ng-repeat에서 입력 필드의 합을 계산하려고합니다. ng-repeat를 사용하여 각 행을 동적으로 표시하고 그 합계를 모두 표시하는 쉬운 방법이 있습니까?ng-repeat 입력 필드에서 합계 계산 AngularJS
도움 주셔서 감사합니다. 정말 감사!
HTML
<div class="row">
<div class="small-12">
<div class="outreach-bar">
<div class="outreach-bar__headline"><h1><span class="icon-pie-chart"></span>Current Total - $<span class="js-form--outreach__step-total">0</span></h1></div>
<div class="outreach-bar__section-wrap outreach-bar__progress">
</div><!--/.outreach-bar__section-wrap -->
</div><!--/.outreach-bar -->
</div><!--/.col -->
</div><!--/.row -->
<div class="carousel--outreach__title"><h1>Step 1 - Home</h1></div>
<div class="form--outreach" ng-controller="AddController">
<div class="row">
<div class="small-12 columns">
<div class="form--outreach__step-title">Current Total - $<span class="js-form--outreach__step-total">0</span></div>
<div class="form--outreach__step-intro">Enter your monthly home expenses below to see how much you spend each month.</div>
</div><!--/.col -->
</div><!--/.row-->
<div class="row" >
<div class="small-6 columns">
<div class="form--outreach__input-label">Mortgage/Rent</div>
<input type="number" class="form--outreach__input js-form--outreach__input" placeholder="Enter Amount"/>
</div><!--/ .col -->
<div class="small-6 columns">
<div class="form--outreach__input-label">Mortgage/Rent</div>
<input type="number" class="form--outreach__input js-form--outreach__input" placeholder="Enter Amount"/>
</div><!--/ .col -->
</div><!--/ .row -->
<div class="row" ng-repeat="rowContent in rows">
<div class="small-6 columns">
<div class="form--outreach__input-label">Mortgage/Rent</div>
<input type="number" class="form--outreach__input js-form--outreach__input" placeholder="Enter Amount"/>
</div><!--/ .col -->
</div><!--/ .row -->
<div class="row">
<div class="small-12 columns">
<div class="form--outreach__add-wrap">
<a class="form--outreach__add-plus" ng-click="addRow()"></a>
<h2>Add Items</h2>
</div><!-- form--outreach__add-wrap -->
</div><!--/.col -->
</div><!--/.row-->
</div><!--/ .form--outreach -->
</div><!--/.carousel--outreach__step -->
[...]
$(document).on('ready', function() {
$('input.js-form--outreach__input').on('input', function(){
var sum=0;
$('input.js-form--outreach__input').each(function(){
if($(this).val() != "")
sum += parseInt($(this).val());
});
$('.js-form--outreach__step-total').html(sum);
});// end of sum functionality
});
var app = angular.module('OutreachCarousel', []);
app.controller('AddController', ['$scope', function ($scope) {
$scope.rows = ['Row'];
$scope.counter = 1;
$scope.addRow = function() {
$scope.rows.push('Row' + $scope.counter);
$scope.counter++;
}
}]);// end of add row functionality
감사합니다. @miquelarranz. 나는 ng-repeat로 각 비용 필드 (Mortgage/Rent)를 동적으로 추가하고 총 비용 필드의 합계를 합계로 추가하는 방법이 있는지 궁금합니다. 지금은 작동하지만 항목 추가 btn을 클릭 할 때 새로운 비용 필드를 추가하면이 새로운 비용 필드의 값이 총계에 추가되지 않습니다. 이게 말이 돼? – tiffsaw
JQuery로 계산하기 때문에 문서가로드 될 때 JQuery 코드가 실행되고 있기 때문에 이것이 효과가 없을 것이라고 생각하기 때문에 (한 번만). 왜 내가 위에서 쓴 것처럼 컨트롤러 내부에서 합계를 계산하지 않습니까? ng-repeat를 사용하여 행을 표시하려면 행 정보가있는 배열이 있어야하고 ng-repeat = "row in row"를 사용해야합니다. 행의 예는 [{name : 'Row 1', amount : 123}, {name : 'Row 2}, amount : 321] 일 수 있습니다. 그런 다음 각 행에 이름과 값 또는 표시 할 내용을 표시합니다. – miquelarranz