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

女生的网名这么多〃 提交于 2019-12-17 19:01:14

问题


I have these two views:

1) foo.html

  <p>Hello {{name}}</p>

2) foo-as-modal.html

<div class="modal-header">
    <h3 class="modal-title">I'm a modal</h3>
</div>
<div class="modal-body">
    <ng-include src="'foo.html'"></ng-include>
</div>
<div class="modal-footer">
    <button class="btn btn-default" ng-click="cancel()">Close</button>
</div>

The controller for foo.html is fooController:

angular.module('myApp').controller('fooController', ['$scope','$uibModalInstance', function($scope,$uibModalInstance) {

     $scope.name = "John"

     $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
     };
}]);

I need to inject $uibModalInstance to fooController for the .dismiss to work

That works great when I invoke foo-as-modal.html as a modal, ie:

    var modalInstance = $uibModal.open({
        templateUrl: 'foo-as-modal.html',
        controller: 'fooController',
        scope: $scope.$new(),
        size: 'lg'
    });

But it throws error when I invoke foo.html as a normal view (via $routeProvider and ng-view directive), ie:

    .when('/foo', {
        templateUrl: 'foo.html',
        controller: 'fooController'
    })

Error is:

 Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- fooController

And the view doesn't display "John" (because of the error)

How can I solve this? I really NEED to reuse foo.html and fooController as a modal and non-modal, because they have lots of stuff (I've simplified here)


EDIT: Here is a plunkr:

https://plnkr.co/edit/9rfHtE0PHXPhC5Kcyb7P


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/37767680/inject-uibmodalinstance-to-a-controllar-not-initiated-by-a-uibmodal

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