AngularJS 1.3.13 - 是否可以向特定的 $resource 添加多个拦截器?
AngularJS 1.3.13 - Is it possible to add multiple interceptors to a particular $resource?
我有一堆不同的拦截器,我只想在特定的 $resource 端点上使用。我阅读了 $resource 的拦截器 属性 并让它工作,但在一些情况下,我希望有两个拦截器用于一个端点响应,就像普通的全局拦截器一样链接。这可能吗?
在用户 $resource 的操作中,我想添加一些类似于我已添加到登录端点的内容:
var actions = {
login: {
method: 'POST',
url: '/api/auth/login',
interceptor: [AuthLoginInterceptor,AuthUserInterceptor]
},
logout: {
method: 'GET',
url: '/api/auth/logout',
interceptor: AuthLogoutInterceptor
},
update: {
method: 'PUT',
interceptor: AuthUserInterceptor
},...
我知道我可以将一个代码包含在另一个代码中,所以最坏的情况我只是重构一个解决方案,但如果可能的话我宁愿保持原样。
这样的事情怎么样?:
{
method: 'POST',
url: '/api/auth/login',
interceptor: {
response: function (config) {
return AuthLoginInterceptor.response(AuthUserInterceptor.response(config));
}
}
}
它似乎在我正在使用的最小测试项目中工作。
根据我上面的评论回答。希望对以后回答这个问题的人有用。
我有一堆不同的拦截器,我只想在特定的 $resource 端点上使用。我阅读了 $resource 的拦截器 属性 并让它工作,但在一些情况下,我希望有两个拦截器用于一个端点响应,就像普通的全局拦截器一样链接。这可能吗?
在用户 $resource 的操作中,我想添加一些类似于我已添加到登录端点的内容:
var actions = {
login: {
method: 'POST',
url: '/api/auth/login',
interceptor: [AuthLoginInterceptor,AuthUserInterceptor]
},
logout: {
method: 'GET',
url: '/api/auth/logout',
interceptor: AuthLogoutInterceptor
},
update: {
method: 'PUT',
interceptor: AuthUserInterceptor
},...
我知道我可以将一个代码包含在另一个代码中,所以最坏的情况我只是重构一个解决方案,但如果可能的话我宁愿保持原样。
这样的事情怎么样?:
{
method: 'POST',
url: '/api/auth/login',
interceptor: {
response: function (config) {
return AuthLoginInterceptor.response(AuthUserInterceptor.response(config));
}
}
}
它似乎在我正在使用的最小测试项目中工作。
根据我上面的评论回答。希望对以后回答这个问题的人有用。