gpt4 book ai didi

javascript - Angular JS 应用程序无法计算正确的总计

转载 作者:行者123 更新时间:2023-11-28 17:42:43 25 4
gpt4 key购买 nike

我正在将 Wcf 服务消费到 Angular JS 应用程序中。我正在尝试在底行添加 Grad Total。我想添加每行值,并且我想添加名称的列是 Amount,如屏幕截图所示。当我运行应用程序时,它没有计算出正确的总数。

这是脚本代码..

var app = angular.module("WebClientModule", [])

.controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {

$scope.OperType = 1;
$scope.Account_Number = "";
$scope.Account_Holder_Name = "";
$scope.Amount = "";
$scope.Sort_Code = "";
$scope.Transcation_Type = "";
$scope.Date = "";

GetAllRecords();
//To Get All Records
function GetAllRecords() {
var promiseGet = myService.getAllStudent();
promiseGet.then(function (pl) { $scope.Users = pl.data },
function (errorPl) {
$log.error('Some Error in Getting Records.', errorPl);
});
}
$scope.grandTotal = function () {
return $scope.Users.reduce(function (ac, cr) {
return ac.Amount + cr.Amount;
});
}


}]);



app.service("myService", function ($http) {

this.getAllStudent = function () {
return $http.get("http://localhost:52098/HalifaxIISService.svc/GetCreateCurrentAccountDepositList");
}
})

这是 HTML 代码..

@{
Layout = null;
}

<!DOCTYPE html>

<html data-ng-app="WebClientModule">
<head>
<meta name="viewport" content="width=device-width" />
<title>TotalDeposit</title>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/RegistrationScript/DepositTotal.js"></script>

</head>
<body>
<table ng-init="items.total = {}" id="tblContainer" data-ng-controller="Web_Client_Controller">
<tr>
<td>
<table style="border: solid 2px Green; padding: 5px;">
<tr style="height: 30px; background-color: skyblue; color: maroon;">
<th></th>
<th>Account Number</th>
<th>Account Holder Name</th>

<th>Amount</th>
<th>Sort Code</th>
<th>Transcation Type</th>
<th>Date</th>

<th></th>
<th></th>
</tr>
<tbody data-ng-repeat="user in Users">
<tr>
<td></td>
<td><span>{{user.Account_Number}}</span></td>
<td><span>{{user.Account_Holder_Name}}</span></td>
<td><span>{{user.Amount}}</span></td>

<td><span>{{user.Sort_Code}}</span></td>

<td><span>{{user.Transcation_Type}}</span></td>
<td><span>{{user.Date}}</span></td>

</tr>
</tbody>
<tr>
<td>Total:</td>

<td>{{grandTotal()}}</td>

</tr>
</table>
</td>
</tr>



</table>
</body>
</html>

这是我运行应用程序时的屏幕截图 Click here to see the out put

最佳答案

一个问题可能是 $scope.Users 未定义,直到 myService.getAllStudent() 解析。因此,您尝试在 undefined 上调用 .reduce 。解决此问题的一种方法是返回“正在加载”或类似的内容,直到设置 $scope.Users:

$scope.grandTotal = function () {
if(!$scope.Users) {
return 'loading';
}

return $scope.Users.reduce(function (ac, cr) {
return ac.Amount + cr.Amount;
});
}

关于javascript - Angular JS 应用程序无法计算正确的总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com