为什么工厂更新时没有计算出的金额更新?

时间:2014-10-20 04:25:05

标签: angularjs

我有一个像这样的简单工厂

angular.module('posBitcoinApp')
.factory('invoiceData', function ($http, $interval) {

var blockchainInfoExchangeRates = {};

var getLatestExchangeRates = function() {

    $http.get('https://api.bitcoinaverage.com/ticker/global/IDR/')
    .success(function(response) {
        blockchainInfoExchangeRates.IDR = response.last;
    });
};

$interval(getLatestExchangeRates, 60000);
getLatestExchangeRates();

return {
    exchangeRates: blockchainInfoExchangeRates
};

});

然后在我的一个控制器中,我......

angular.module('posBitcoinApp')
.controller('InvoiceCtrl', function ($scope, $http, $interval, $location, invoiceData) {

$scope.invoiceData = invoiceData;
$scope.invoiceData.btcAmount = parseFloat($scope.invoiceData.idrAmount / $scope.invoiceData.exchangeRates.IDR).toFixed(8);

所以汇率每时每刻都会更新。但是,计算出的BTC值($ scope.invoiceData.btcAmount)不会自动更新。我错过了什么?需要注意什么? $ scope.apply()在哪里?

感谢。

1 个答案:

答案 0 :(得分:0)

即使您在工厂刷新数据,也不会在返回数据时反映出来。而不是你必须每隔一分钟刷新scope.invoiceData.btcAmount。

Var updateBTC = function(){
//call to factory if you want to avoid having an interval inside factory to update data and use the result in the below statement.
   $scope.invoiceData.btcAmount = parseFloat($scope.invoiceData.idrAmount / $scope.invoiceData.exchangeRates.IDR).toFixed(8);
};

$interval(updateBTC, 60000);