Nikhil
Nikhil

Reputation: 1186

Bootstrap uib modal with multiple controllers

I am trying to use a bootstrap modal and I am passing the main controller in the $uibModal.open({controller : "mainCtrl"}).

So far its all good now I want to have several tabs in the modal and I want each tab to have its own controller using ng-controller. This is where I am getting the error : [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- tabController which is expected because it seems we cannot have ng-controller inside uibModalInstance. Is there an alternate way of achieving this?

Upvotes: 1

Views: 1470

Answers (1)

lin
lin

Reputation: 18402

It should work for you like in this |- demo fiddle - |- demo plnkr with template files -| I've create for you. Please compare this solution carefully with yours. You can have as many ng-controller defined in your uibModal template as you want. Also ensure that your controller is a part of the same module.

View

 <div ng-app="ui.bootstrap.demo">
  <div ng-controller="ModalDemoCtrl">
    <script type="text/ng-template" id="myModalContent.html">
      <div class="modal-header" ng-controller="ModalDemoSecondCtrl">
        <h3 class="modal-title">I'm a modal!</h3>
      </div>
      <div class="modal-body" ng-controller="ModalDemoThirdCtrl">
        Demo form submit:
        <br/>
        <form ng-submit="ok()">
          <div class="input-group animated fadeIn">
            <input type="text" class="form-control finderBar" ng-model="searchTerm" placeholder="City, State..." autofocus>
            <span class="input-group-btn">
            <button class="btn btn-default" type="button" ng-click="ok()">Go!</button>
        </span>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
      </div>
    </script>
    <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
  </div>
</div>

AngularJS application

 angular.module('ui.bootstrap.demo', ['ui.bootstrap'])
  .controller('ModalDemoCtrl', function($rootScope, $scope, $log, $uibModal) {

    $scope.open = function(size, template) {
      var modalInstance = $uibModal.open({
          animation: $scope.animationsEnabled,
          templateUrl: template || 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          size: size
        });
    };

    $scope.toggleAnimation = function() {
      $scope.animationsEnabled = !$scope.animationsEnabled;
    };

  }).controller('ModalDemoSecondCtrl', function($rootScope, $scope, $log, $uibModal) {
  }).controller('ModalDemoThirdCtrl', function($rootScope, $scope, $log, $uibModal) {});

angular.module('ui.bootstrap.demo')
 .controller('ModalInstanceCtrl', function($scope, $uibModalInstance) {
    $scope.ok = function() {
      $uibModalInstance.dismiss('cancel');
    };

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

Upvotes: 1

Related Questions