【发布时间】:2014-11-05 17:10:08
【问题描述】:
我正在尝试实现延迟授权,仅当用户触发对需要身份验证的 API 的调用时才会引入登录对话框。我正在使用引导程序ui.bootstrap.modal(和模态中的ui.bootstrap.alert)。问题是这些指令明确指定以下teamplateUrl:
- template/modal/backdrop.html(在
modal.jshere) - template/modal/window.html(在
modal.jshere) - template/alert/alert.html(在
alert.jshere)
像这样:
.directive('modalBackdrop', ['$timeout', function ($timeout) {
return {
restrict: 'EA',
replace: true,
templateUrl: 'template/modal/backdrop.html',
link: function (scope, element, attrs) {
/* ... */
}
};
}])
每次我调用 $modal.open() 并且 ui-bootstrap 为新的模态窗口构建 DOM 时,Angular 都会尝试通过 $http 服务解析这些 url,即使模板已经通过 $templateCache.put 方法或通过添加<script> 标签。
这基本上导致了我的拦截器中的无限递归,它试图在request 重载中为上面的 url 引入登录对话框。
这是我的拦截器的简化版本:
.config(['$provide', '$httpProvider', function($provide, $httpProvider) {
$provide.factory('testInterceptor', ['$injector', function($injector) {
return {
'request': function(config) {
var auth = $injector.get('authService');
var modal = $injector.get('$modal');
if (!auth.LoggedIn) {
var loginModal = modal.open({
template: 'Login screen <alert type="warning">some message</alert>',
});
return loginModal.result;
}
return config;
}
}}]);
工作演示是here
谁能建议不涉及对ui.bootstrap.modal 和ui.bootstrap.alert 中使用的templateUrls 进行硬编码的方法?
我还在 github 上将此报告为 issue。
【问题讨论】:
-
你为什么使用 $injector 而不是仅仅将这些东西作为依赖项传递?即
$injector.get('$modal') -
这可能不起作用,因为拦截器正在
config部分中注册,该部分仅支持提供者注入(请参阅documentation)
标签: angularjs angularjs-directive angular-ui angular-ui-bootstrap angularjs-http