2014-10-20 77 views
0

我有一個簡單的工廠這樣工廠更新時爲什麼沒有計算的數量更新?

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()在哪裏?

謝謝。

+0

您的控制器應該監視'invoiceData.exchangeRates'。 – leesei 2014-10-20 05:39:45

回答

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);