我的 app.js 看起来像这样。我需要全局处理 AJAX 错误。如果我只使用console.log,这段代码就可以正常工作。但是,我需要使用 $modal 触发模态对话框,但不知道如何实现。
任何帮助表示赞赏。谢谢!
define(['jquery','angularAMD', 'modernizr' ,'angular-ui-router'], function ($, angularAMD) {
var app = angular.module('ABC', ['ui.router','angular.css.injector']),
ngAMD = angularAMD(app);
app.run(function($rootScope){
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from) {
$rootScope.previousState = from.name;
$rootScope.currentState = to.name;
});
});
app.config(function($stateProvider, $urlRouterProvider, $provide, $httpProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('first',
ngAMD.route({
url:'/first',
templateUrl: 'app/partials/first.html',
controller : 'firstCrtl'
})
)
.state('default',
ngAMD.route({
url:'/',
templateUrl: 'app/partials/default.html',
controller : 'defaultCrtl'
})
);
$provide.factory('globalAjaxInterceptor', function($q) {
return {
// On request success
request: function(config) {
return config || $q.when(config);
},
// On request failure
requestError: function(rejection) {
return $q.reject(rejection);
},
// On response success
response: function(response) {
if(response.data.status === "FAILURE" && response.data.failueCode === "F003") {
console.log('Trigger Modal');
}else if(response.data.status == "SUCCESS"){
console.log('Response Address Successs');
}
// Return the response or promise.
return response || $q.when(response);
},
// On response failture
responseError: function(rejection) {
// Return the promise rejection.
switch (rejection.status) {
case 404:
console.log('Destinaion not found!')
break;
default:
console.log('Trigger Modal');
}
return $q.reject(rejection);
}
};
});
// Add the interceptor to the $httpProvider.
$httpProvider.interceptors.push('globalAjaxInterceptor');
});
ngAMD.bootstrap();
return app;
});