gpt4 book ai didi

javascript - angularjs从服务获取错误消息到 Controller

转载 作者:行者123 更新时间:2023-12-03 07:42:27 25 4
gpt4 key购买 nike

使用 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 调用服务以获取费率。

.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;
}
}])

兑现 promise 后,费率在 Controller 中显示良好。但是,在收到错误后,它们不会从服务传输到 Controller 。 (我更改了 REST URL 以使服务返回 404 响应)。我做错了什么?

最佳答案

当前您正在从异步函数外部返回异步调用响应,从技术上讲您不应该这样做。异步代码总是在其 promise/callback 函数内获得响应。

但就您而言,它正在工作,因为您正在返回带有引用的对象。如果你看下面的代码,return语句已经携带了self.rates对象引用,所以在返回时即使它是空白的,它也会获得更新的值一次self.rates 被填满。因此,您无需担心费率更新。但是对于 error 来说,同样的事情是行不通的,因为它是原始的数据类型,比如 var error = null ,所以当你返回值时,它会是 null 。 >(因为异步响应尚未完成)。

return {
rates: self.rates, //passed by reference
errors: error //passed by primitive type
}

因此,为了解决这个问题,您还可以将错误设置为 object 类型,以便它的引用将与引用对象一起传递,例如 varErrors = [] & 当错误发生时,使用 .push

将错误消息推送到该数组中<小时/>

但我不建议采用上述方法,我宁愿使用 promise 模式并维护正确的代码调用堆栈。基本上,您需要从 search 方法返回 Promise,然后放置 .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
}
})
}
}
}
])

Controller

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

关于javascript - angularjs从服务获取错误消息到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com