Angularjs 未捕获错误:[$injector:cdep]在工厂中使用ngdialog
我想使用ngdialog处理401状态,但我得到了错误:uncaughterror:[$injector:cdep]Angularjs 未捕获错误:[$injector:cdep]在工厂中使用ngdialog,angularjs,http-status-code-401,ng-dialog,Angularjs,Http Status Code 401,Ng Dialog,我想使用ngdialog处理401状态,但我得到了错误:uncaughterror:[$injector:cdep] angular.module('ws.site.master', [ 'ngResource', 'ngCookies', 'ngSanitize', 'ngAnimate', 'ui.router', 'ngDialog' ]); 这里我添加了一个工厂来处理401状态 angular.module
angular.module('ws.site.master', [
'ngResource',
'ngCookies',
'ngSanitize',
'ngAnimate',
'ui.router',
'ngDialog'
]);
这里我添加了一个工厂来处理401状态
angular.module('ws.site.master').factory('authHttpResponseInterceptor',['$q','$location','ngDialog',function($q,$location,ngDialog){
return {
response: function(response){
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
},
responseError: function(rejection) {
if (rejection.status === 401) {
console.log("Response Error 401",rejection);
ngDialog.open({
template: '/common/templates/at.modal.security.html',
className: 'ngdialog-theme-default modal-security',
closeByDocument: false,
closeByEscape: false,
trapFocus: false,
controller: ['$scope', function($scope) {
$scope.defaultView = defaultView || 'login';
}]
});
$rootScope.isSecurityModal = true;
}
return $q.reject(rejection);
}
}
}]);
此处将authHttpResponseInterceptor添加到$httpProvider
angular.module('ws.site.master').config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', 'ROUTE',
function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, ROUTE) {
$locationProvider.html5Mode(true);
angular.forEach(ROUTE, function(_route, _name){
$stateProvider.state(_name, _route);
});
$urlRouterProvider.otherwise('/');
$httpProvider.interceptors.push('authHttpResponseInterceptor');
}
]);
错误:$injector:cdep-循环依赖项 请检查有关的文件
您将“ngDialog”注入两次,先注入主模块,然后注入工厂,所有这些都在同一个“ws.site.master”中。对于对话框窗口,您必须在html头中包含引导TPL。应用程序应如下所示:
angular.module('myModule', [ 'ui.bootstrap']);
在控制器或工厂有:
app.controller('myController', function($scope, $modal) {
...
$scope.openPopup = function() {
$scope.modalInstance = $modal
.open({
animation : true,
backdrop : 'static',
templateUrl : 'yourTemplatePath/template.html',
controller : 'yourPopupController'
});
$scope.modalInstance.result.then(function(returnResult) {
$scope.returnResult = returnResult;
});
};
我试图删除angular.module中的ngdialog,但仍然出错。如果我删除所有的对话框,就会有OK。但是我需要使用它:(是的~我找到了解决方案~~~~在rootScope中编写一个方法来使用ngdialog,并在authHttpResponseInterceptor中使用它~~~~~~ Hohohot OP询问的是ngdialog/http拦截器绑定,而不是ui.bootstrap的对话框。
app.controller('myController', function($scope, $modal) {
...
$scope.openPopup = function() {
$scope.modalInstance = $modal
.open({
animation : true,
backdrop : 'static',
templateUrl : 'yourTemplatePath/template.html',
controller : 'yourPopupController'
});
$scope.modalInstance.result.then(function(returnResult) {
$scope.returnResult = returnResult;
});
};