Inject $uibModalInstance to a controllar not initiated by a $uibModal

后端 未结 3 446
猫巷女王i
猫巷女王i 2020-12-09 23:13

I have these two views:

1) foo.html

  

Hello {{name}}

2) foo-as-modal.html

相关标签:
3条回答
  • 2020-12-09 23:41

    Well I solved this way:

    1. Removed the injection $uibModalInstance from fooController
    2. When invoking the modal, I pass the modalInstance as a variable to the modal's scope:

      var modalScope = $scope.$new();
      
      var modalInstance = $uibModal.open({
          templateUrl: 'foo-as-modal.html',
          controller: 'fooController',
          scope: modalScope
      });
      
      modalScope.modalInstance = modalInstance;
      
    3. Dismiss the modal with the scope variable:

      $scope.modalInstance.dismiss('cancel');  // instead of $uibModalInstance.dismiss(..)
      

    Here is a fork of the original plunkr, with this solution: https://plnkr.co/edit/ZasHQhl6M5cCc9yaZTd5

    0 讨论(0)
  • 2020-12-09 23:57

    One way to do this is to resolve $uibModalInstance in the route with null (or an empty string/or whatever you please) and inject it into the controller. If the html page isn't opened as a modal, you still have $uibModalInstance. If it's opened as a modal, $uibModalInstance is automatically injected.

    0 讨论(0)
  • 2020-12-09 23:58

    You can not use the same controller for a page view and for a modal window. At least, untill controller depends on $uibModalInstance.

    $uibModalInstance can be injected only in modals.

    0 讨论(0)
提交回复
热议问题