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

后端 未结 3 456
猫巷女王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

提交回复
热议问题