我有一个AngularJS 1.0.7 webapp。在我的service.js中,我有一个SearcherService
服务尝试同步调用CurrencyService
服务,但有些东西无法正常工作。
.factory('SearcherService', function(CurrencyService, LocationService) {
return {
var currency = LocationService.getCurrency();
if (currency == undefined) {
var currency = CurrencyService.getCurrency();
// do more thing after currency is got
}
};
})
.factory('CurrencyService', function(LocationService, Currency) {
return {
getCurrency: function() {
Currency.query(function success(result) {
var currencies = result;
LocationService.setCurrency(currency[0]);
return currency[0];
});
}
};
});
更新 实际上没有错误堆栈跟踪。但我的预期行为是在继续执行//执行更多操作之前从CurrencyService.getCurrency获取货币值。我不知道为什么我有其他服务调用不会继续,直到服务方法调用完全执行但我没有看到差异。另请参阅我的货币服务。 我想做这样的事情。但这不起作用。它永远不会进入回调函数。
var currency = LocationService.getCurrency();
if (currency == undefined) {
CurrencyService.getCurrency().then(function(result) {
currency = result;
// do more thigs
});
}
另见我的货币服务:
.factory('Currency',
function($resource, SERVER_URL){
var currencies =
$resource('http://' + SERVER_URL +'/:action', {action:'currencies'}, {
query: {method:'GET', isArray: true},
getConversionRate: {method:'GET', params:{action:'getConversionRate'}, isArray: false}
});
return currencies;
}
)