我的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;
});
答案 0 :(得分:0)
他的问题的解决方案是修复Circular dependency error
或Cdep
的解决方案。
修复是使用$modal
明确注入$injector
。解决方案如下所示 -
responseError: function(rejection) {
// Return the promise rejection.
switch (rejection.status) {
case 404:
console.log('Destinaion not found!')
break;
default:
$injector.invoke(function($modal) {
$rootScope.errorOverlay($modal);
});
}
return $q.reject(rejection);
}