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

旧城冷巷雨未停 提交于 2019-11-28 08:48:53

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

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.

Tosalisu

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!