I have a global error handler function for $http request.
对于$http请求,我有一个全局错误处理函数。
var errorHandler = function (err) {
// log the error
return {status: err.status, data: {result: -1}}
}
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, errorHandler)
}
How can I pass the variable i to errorHandler? I know I can do this:
如何将变量I传递给errorHandler?我知道我能做到:
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, function (err) {
// log the error
console.log('i', i) // access i
return {status: err.status, data: {result: -1}}
})
}
But if I want use a global errorHandler, what should I do?
但是如果我想使用全局errorHandler,我应该怎么做呢?
==============
= = = = = = = = = = = = = =
Update:
更新:
According to Leandro Zubrezki's answer, we can make it in this way:
根据Leandro Zubrezki的回答,我们可以这样做:
var errorHandler = function (err, i) {
// log the error
return {status: err.status, data: {result: -1}}
}
var get = function (url) {
var i = 1 // I have a temp variable here, and want to pass it to errorHandler
return $http({
method: 'GET',
url: ***,
timeout: 5000,
}).then(function (response) {
// do some thing
return response
}, function(error){
errorHandler(error, i)
})
}
This is a stupid question from newbie. :)
这是一个新手提出的愚蠢问题。:)
1 个解决方案
#1
2
Do it like this:
这样做:
function(error) {
errorHandler(error, i);
}
#1
2
Do it like this:
这样做:
function(error) {
errorHandler(error, i);
}