【问题标题】:Angularjs - $uibModal httpProvider interceptorsAngularjs - $uibModal httpProvider 拦截器
【发布时间】:2016-03-11 22:25:35
【问题描述】:

我想设置一个 httpInterceptor 以在 http 请求失败时显示一个常见的模式对话框。我在模态对话框中使用https://angular-ui.github.io/bootstrap/

我试过了

app.config(function ($httpProvider, $uibModal) { ...

但收到错误 [$injector:modulerr] 无法实例化模块应用程序,原因是: 错误:[$injector:unpr] 未知提供者:$uibModal

This answer表示配置的时候只能传provider,于是我试了

app.config(function ($httpProvider, $uibModalProvider) {

在我想要打开模式的地方起作用

var modalInstance = $uibModalProvider.open(

并且我得到对象不支持打开的属性或方法的错误。我如何从提供者到模态实例,或者有其他方法可以实现吗?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    使用$injector 服务获取$uibModal 的实例。

    app.config(function($httpProvider) {
    
      //To configure an interceptor you have to push a function
      //or name of a registered service into the array. The function
      //or service is where you call $injector.get();
      $httpProvider.interceptors.push(function($injector) {
        return {
          responseError: function(res) {
            var templateUrl = 'http://badurl'; //This can cause infinite recursion!
            if (res.config.url === templateUrl) return null; //One way to prevent infinite recursion.
            var $uibModal = $injector.get('$uibModal');
            var modalInstance = $uibModal.open({
              templateUrl: templateUrl
            });
            return res;
          }
        }
      })
    
    });
    

    但要小心导致无限递归。

    【讨论】:

    • 这给了我一个错误:[$injector:unpr] Unknown provider: $uibModal on the line var $uibModal = $injector.get('$uibModal');
    • 我使用 var app = angular.module('app', ['ngAnimate', 'ui.bootstrap']) 配置应用程序
    • 这意味着 ui-bootstrap 未正确包含在您的模块中。
    • ui-bootstrap 库是否正确包含在您的索引页中?
    • 是的,在我尝试将模态逻辑集中到 httpInterceptor 之前,ui-bootstrap 库在此页面上工作。
    【解决方案2】:

    查看以下示例:

    (function(angular) {
      'use strict';
      var module = angular.module('app', ['ngAnimate', 'ui.bootstrap']);
      
      module.controller('appController', ['$http', '$log', function($http, $log) {
        var vm = this;
        vm.sendRequest = sendRequest;
        
        function sendRequest(){
          $log.info('Try sending a request');
          $http.get('/fake/');
        }
    
      }]);
      
      module.controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
        $scope.ok = function () {
          $uibModalInstance.dismiss('cancel');
        };
      }]);
    
      module.factory('myHttpInterceptor', ['$log', '$injector', function($log, $injector) {  
        var interceptor = {
          'responseError': function(config) {
            $log.info('Request error');
          
            // injecting $uibModal directly cause Circular Dependency error
            // following method is a fix of it
            $injector.get('$uibModal').open({
              templateUrl: 'myModalContent.html',
              controller: 'ModalInstanceCtrl',
              size: 'sm'
            });
            
            return config;
          }
        };
        return interceptor;
      }]);
      
      module.config(['$httpProvider', function($httpProvider) {  
        $httpProvider.interceptors.push('myHttpInterceptor');
      }]);
      
    })(window.angular);
    body { padding: 20px; }
    <!doctype html>
    <html lang="en">
    <head>
      <title>Interceptor & $uibModal sample</title>
      <meta charset="UTF-8">
      
      <!-- AngularJS -->
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
      <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.4.js"></script>
      <!-- Bootstrap -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" >
      
    </head>
    <body ng-app="app">
      
      <div class="panel panel-default" ng-controller="appController as vm">
        
        <script type="text/ng-template" id="myModalContent.html">
            <div class="modal-header">
                <h3 class="modal-title">Error!</h3>
            </div>
            <div class="modal-body">
                Hello
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
            </div>
        </script>
    
        <div class="panel-heading">
          Send failing request
        </div>
    
        <div class="panel-body">
          <button type="button" class="btn btn-primary" ng-click="vm.sendRequest()">Send request</button>
        </div>
        
      </div>
      
    </body>
    </html>

    【讨论】:

    • 我尝试了这种方法,但无法弄清楚如何摆脱循环依赖。您的解决方案为我指明了方向。再次感谢。
    • 谢谢!您是否介意围绕为什么添加一些上下文,而不是将提供程序添加到注入列表中?
    • 是什么导致模态关闭?
    猜你喜欢
    • 2014-11-28
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多