在angular最佳实践的ppt上看见这段代码:
http://trochette.github.io/Angular-Design-Patterns-Best-Practices/#/configuring_httpproviders
angular.module('notes',[])
.config(["$httpProvider",
function ($httpProvider) {
var httpStatusCodeInterceptorFactory = function ($q) {
function onSuccess(response){
if("success_condition"){
return response.data;
}else{
//Show your global error dialog
$q.reject(response.data);//Very important to reject the error
}
};
function onError(response){
//Show your global error dialog
$q.reject(response);//Very important to reject the error
};
return function (promise) {
return promise.then(onSuccess,onError);
};
};
//Activate your interceptor
$httpProvider.responseInterceptors.push(httpStatusCodeInterceptorFactory);
}])
config
里面的代码看不太懂,是不是之前老版本的函数式写法?
要是改成现在的写法怎么写呢?
好吧,我傻了,确实是比较老的版本,1.0的
新版的直接建一个拦截器service,然后拦截就可以了
具体参考这篇文:http://my.oschina.net/ilivebox/blog/290881?p=1