Angularjs 为什么';工厂更新时,是否更新计算的金额?
我有一个这样简单的工厂Angularjs 为什么';工厂更新时,是否更新计算的金额?,angularjs,Angularjs,我有一个这样简单的工厂 angular.module('posBitcoinApp') .factory('invoiceData', function ($http, $interval) { var blockchainInfoExchangeRates = {}; var getLatestExchangeRates = function() { $http.get('https://api.bitcoinaverage.com/ticker/global/IDR/')
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()在什么地方
谢谢。即使您在factory上刷新数据,也不会在返回数据时反映出来。相反,您必须每隔一分钟刷新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);
您的控制器应该监视invoiceData.exchangeRates。