从内部解雇$ uibModal

Mov*_*aev 3 angularjs angular-ui-bootstrap

我正在使用Bootstrap的$ uibModal在我的web应用程序中制作表单.一切正常,除了在显示对话后我无法关闭对话框.

我花了两天时间试图不在这里发布这么简单的问题,特别是当我收到的同样错误的答案很多时,即

" 未知提供者:$ uibModalInstanceProvider < - $ uibModalInstance < - ModalInstanceCtrl ".

为了使用大量应用程序轻松操作,我将代码分成单独的文件,我在htmls中使用'controller as'样式,因此,代码中没有$ scope.

无论我做什么,我都会收到我之前提到的这个错误.我需要的是在用户成功登录后关闭对话框.

index.html的:

...
    <script src="rf/angular.min.js"></script>
    <script src="rf/angular-animate.js"></script>
    <script src="rf/angular-touch.js"></script>
    <script src="rf/ui-bootstrap-tpls.js"></script>

    <!-- Project files -->
    <script src="application.js" type="text/javascript"></script> 
    <script src="frmLogin.js"></script>
</head>
<body>
    <!-- Load things into this division -->
    <div ng-include src="loaderCtrl.cFragment" ng-app="dgis" ng-controller="applicationController as loaderCtrl"></div>
</body>
Run Code Online (Sandbox Code Playgroud)

的application.js

var myApp = angular
  .module('dgis', ['ui.bootstrap'])
  .controller("applicationController", ['$http', '_gl', '$uibModal', function ($http, _gl, $uibModal) {
      _gl.AppReference = this;

      // Enable animations
      this.animationsEnabled = true;

      // Declare modal window
      this.showFrmLogin = function (size) {
         var modalInstance = $uibModal.open({
              animation: this.animationsEnabled,
              templateUrl: 'frmLogin.html',
              controller: 'ModalInstanceCtrl',
              size: size,
              backdrop: 'static',
              keyboard: false,
              resolve: {
                  items: function () {
                      return this.items;
                  }
              }
          });
      };

      this.showFrmLogin();
Run Code Online (Sandbox Code Playgroud)

frmLogin.js

myApp.controller('ModalInstanceCtrl', ['_gl', '$uibModalInstance', function (_gl, $uibModalInstance) {

    this.login = function () {        
        angular.forEach(some_array, function (element) {
            if (something == element)                   
               // This code executes successfully and shows the page
               _gl.AppReference.cFragment = "frmMain.html";
               // This line does not close the dialog
               $uibModalInstance.close('a');
            }
        });
    }
}]);
Run Code Online (Sandbox Code Playgroud)

_gl - 是我用来保存全局变量的服务

小智 12

使用$ rootScope

初始化模态时使用$ rootScope.modalInstance

您可以从应用程序的任何位置访问它.希望这可以帮助


  1. 删除$ uibModalInstance引用
  2. 而不是"var modalInstance = $ uibModal.open({"

    使用"$ rootScope.modalInstance = $ uibModal.open({

  3. 而不是$ uibModalInstance.close('a');"

    使用"$ rootScope.modalInstance.close('a');"

  • 我用 var modalInstance = $uibModal.open({ ...options }); 然后我用 modalInstance.close(); 关闭了它。 (2认同)