我正在出租车租赁服务申请,其中包括租用出租车的功能。通过租用它,数据库条目被制作成某个出租车的历史数组,包括买方的姓名,驱动器的长度和根据租金长度的价格。使用间隔调用计算租赁收益总和并将结果传递给html页面的函数,但是在刷新页面之前它不会注册数据库中所做的更改,但我想要更改自动注册,无需刷新。
这是代码:
var getSumOfAll = function(){
curProf=0;
//get sum of all taxies
sumAll = $scope.getTotalAll($scope.taxies);
//get the current earnings of the active taxies
for(var i = 0; i<$scope.taxies.length; i++){
if(!$scope.taxies[i].available && timeSpent($scope.taxies[i])>=$scope.taxies[i].history[0].length){
$scope.updateRent($scope.taxies[i], $scope.taxies[i]._id, $scope.taxies[i].history[0].length);
}
if(!$scope.taxies[i].available){
curProf += $scope.getCurrentProfit($scope.taxies[i]);
}
}
console.log($scope.taxies[0].history[0])
//show current gains
$scope.gains = curProf+sumAll;
}
getTotalAll代码:
$scope.getTotalAll = function(taxies){
var total = 0;
//console.log(taxies.length)
for(var i = 0; i < taxies.length; i++){
for(var j = 0; j < taxies[i].history.length; j++){
if(taxies[i].history[j].price) total += taxies[i].history[j].price;
}
}
return total;
}
在html网站中使用:
<div ng-controller="HeaderController" class="md-padding" style="text-align: center;">
<div ng-init="getTaxies();">
<a href="#!"><img src="/img/logo/taxi.png" id="logo"></a>
<md-toolbar>
<div class="md-toolbar-tools">
<md-truncate id="sum">Sum of gains: {{gains}}</md-truncate>
<span flex></span>
<md-button class="md-raised" ng-disabled="0>=gains" ng-click="buyTaxi()">Buy a taxi</md-button>
<md-button class="md-raised" ng-click="addTaxi()">Add a taxi</md-button>
</div>
</md-toolbar>
</div>
答案 0 :(得分:0)
由于sumAll函数中的值保持不变,因此无法检索更新的数据。我通过调用工厂函数每次检索更新的数据来解决这个问题。 (返回$ http.get('/ api / taxies'));
var getSumOfAll = () => {
dataFactory.getTaxies().then(function(response){ //make a get req from this address
taxies = response.data;
curProf=0;
//show current gains
$scope.gains = getTotalAll(taxies);
});
}