angularjs 获取从服务到控制器的错误信息
angularjs get the error message from the service to the controller
使用 REST 获取费率的工厂
.factory('rateResource', ['$resource', function ($resource) {
return $resource('/rates/:currency/:effectiveDate', {
currency: '@currency',
effectiveDate: '@effectiveDate'
});
}])
调用工厂以获取资源和检索费率以及错误(如果有)的服务。
.service('RateService', ['rateResource', '$rootScope',
function (rateResource, $rootScope) {
var self = this;
self.rates = [];
self.search = function (baseCurrency, effectiveDate) {
self.rates = [];
var error = null;
self.baseCurrency = baseCurrency;
self.effectiveDate = effectiveDate;
if (baseCurrency) {
rateResource.query({currency: baseCurrency, effectiveDate: effectiveDate})
.$promise.then(function (rates) {
if (rates) {
angular.forEach(rates, function (rate) {
rate.maintTs = $rootScope.formatTimestampToHHMMSS(rate.maintTs);
rate.editable = false;
self.rates.push(rate);
});
self.processing = false;
}
}, function (response) {
self.processing = false;
error = 'Processing failed due to '+response.status;
console.log(error);
})
return {
rates: self.rates,
errors: error
}
}
}
}]);
控制器调用服务获取费率。
.controller('RateController', ['RateService',
function (rateService) {
var self = this;
self.baseCurrency = rateService.getBaseCurrency();
self.effectiveDate = rateService.getEffectiveDate();
self.rates = rateService.getRates();
//make the call from the controller
self.search = function () {
var response = rateService.search(self.baseCurrency, self.effectiveDate.yyyyMMdd());
self.rateRecords = response.rates;
self.errors = response.errors;
}
}])
兑现承诺后,控制器中的费率显示正常。但是,在收到错误后,它们不会从服务转移到控制器。 (我更改了 REST URL 以使服务 return 成为 404 响应)。我做错了什么?
目前您正在 return 从异步函数外部进行异步调用响应,从技术上讲您不应该这样做。异步代码总是在它们的 promise
/callback
函数中得到响应。
但在您的情况下,它可以正常工作,因为您正在 returning 带有它引用的对象。如果你看下面的代码,return
语句已经携带了 self.rates
对象引用,所以当 returning 即使它是空白的,它也会得到更新一次的值 self.rates
被填满。因此,您无需担心 rates
更新。但是对于错误来说同样的事情是行不通的,因为它是原始的 datatype
就像 var error = null
,所以当你 returning 值时它会是 null
(因为异步响应没有' t 完成)。
return {
rates: self.rates, //passed by reference
errors: error //passed by primitive type
}
因此,为了解决这个问题,您还可以将错误设置为 object
类型,以便它的引用将与引用对象一起传递,例如 var errors = []
& 当发生错误时推送错误消息使用 .push
进入该数组
但我不推荐使用上述方法 ,我宁愿使用 promise 模式并维护适当的代码调用堆栈。基本上为此,您需要 return 从 search
方法承诺,然后放置 .then
函数等待解析该函数。
服务
.service('RateService', ['rateResource', '$rootScope',
function(rateResource, $rootScope) {
var self = this;
self.rates = [];
self.search = function(baseCurrency, effectiveDate) {
self.rates = [];
var error = null;
self.baseCurrency = baseCurrency;
self.effectiveDate = effectiveDate;
if (baseCurrency) {
return rateResource.query({
currency: baseCurrency,
effectiveDate: effectiveDate
})
.$promise.then(function(rates) {
if (rates) {
angular.forEach(rates, function(rate) {
rate.maintTs = $rootScope.formatTimestampToHHMMSS(rate.maintTs);
rate.editable = false;
self.rates.push(rate);
});
self.processing = false;
}
//returning object from promise
return {
rates: self.rates,
errors: error
}
}, function(response) {
self.processing = false;
error = 'Processing failed due to ' + response.status;
console.log(error);
//returning object from promise
return {
rates: self.rates,
errors: error
}
})
}
}
}
])
控制器
//make the call from the controller
self.search = function() {
var response = rateService.search(self.baseCurrency, self.effectiveDate.yyyyMMdd()).then(function() {
self.rateRecords = response.rates;
self.errors = response.errors;
});
}
使用 REST 获取费率的工厂
.factory('rateResource', ['$resource', function ($resource) {
return $resource('/rates/:currency/:effectiveDate', {
currency: '@currency',
effectiveDate: '@effectiveDate'
});
}])
调用工厂以获取资源和检索费率以及错误(如果有)的服务。
.service('RateService', ['rateResource', '$rootScope',
function (rateResource, $rootScope) {
var self = this;
self.rates = [];
self.search = function (baseCurrency, effectiveDate) {
self.rates = [];
var error = null;
self.baseCurrency = baseCurrency;
self.effectiveDate = effectiveDate;
if (baseCurrency) {
rateResource.query({currency: baseCurrency, effectiveDate: effectiveDate})
.$promise.then(function (rates) {
if (rates) {
angular.forEach(rates, function (rate) {
rate.maintTs = $rootScope.formatTimestampToHHMMSS(rate.maintTs);
rate.editable = false;
self.rates.push(rate);
});
self.processing = false;
}
}, function (response) {
self.processing = false;
error = 'Processing failed due to '+response.status;
console.log(error);
})
return {
rates: self.rates,
errors: error
}
}
}
}]);
控制器调用服务获取费率。
.controller('RateController', ['RateService',
function (rateService) {
var self = this;
self.baseCurrency = rateService.getBaseCurrency();
self.effectiveDate = rateService.getEffectiveDate();
self.rates = rateService.getRates();
//make the call from the controller
self.search = function () {
var response = rateService.search(self.baseCurrency, self.effectiveDate.yyyyMMdd());
self.rateRecords = response.rates;
self.errors = response.errors;
}
}])
兑现承诺后,控制器中的费率显示正常。但是,在收到错误后,它们不会从服务转移到控制器。 (我更改了 REST URL 以使服务 return 成为 404 响应)。我做错了什么?
目前您正在 return 从异步函数外部进行异步调用响应,从技术上讲您不应该这样做。异步代码总是在它们的 promise
/callback
函数中得到响应。
但在您的情况下,它可以正常工作,因为您正在 returning 带有它引用的对象。如果你看下面的代码,return
语句已经携带了 self.rates
对象引用,所以当 returning 即使它是空白的,它也会得到更新一次的值 self.rates
被填满。因此,您无需担心 rates
更新。但是对于错误来说同样的事情是行不通的,因为它是原始的 datatype
就像 var error = null
,所以当你 returning 值时它会是 null
(因为异步响应没有' t 完成)。
return {
rates: self.rates, //passed by reference
errors: error //passed by primitive type
}
因此,为了解决这个问题,您还可以将错误设置为 object
类型,以便它的引用将与引用对象一起传递,例如 var errors = []
& 当发生错误时推送错误消息使用 .push
但我不推荐使用上述方法 ,我宁愿使用 promise 模式并维护适当的代码调用堆栈。基本上为此,您需要 return 从 search
方法承诺,然后放置 .then
函数等待解析该函数。
服务
.service('RateService', ['rateResource', '$rootScope',
function(rateResource, $rootScope) {
var self = this;
self.rates = [];
self.search = function(baseCurrency, effectiveDate) {
self.rates = [];
var error = null;
self.baseCurrency = baseCurrency;
self.effectiveDate = effectiveDate;
if (baseCurrency) {
return rateResource.query({
currency: baseCurrency,
effectiveDate: effectiveDate
})
.$promise.then(function(rates) {
if (rates) {
angular.forEach(rates, function(rate) {
rate.maintTs = $rootScope.formatTimestampToHHMMSS(rate.maintTs);
rate.editable = false;
self.rates.push(rate);
});
self.processing = false;
}
//returning object from promise
return {
rates: self.rates,
errors: error
}
}, function(response) {
self.processing = false;
error = 'Processing failed due to ' + response.status;
console.log(error);
//returning object from promise
return {
rates: self.rates,
errors: error
}
})
}
}
}
])
控制器
//make the call from the controller
self.search = function() {
var response = rateService.search(self.baseCurrency, self.effectiveDate.yyyyMMdd()).then(function() {
self.rateRecords = response.rates;
self.errors = response.errors;
});
}