Bootstrap 具有多个控制器的 uib 模态

Bootstrap uib modal with multiple controllers

我正在尝试使用 bootstrap modal 并且我正在通过 $uibModal.open({controller : "mainCtrl"}) 中的主控制器。

到目前为止一切都很好,现在我想在模式中有几个选项卡,我希望每个选项卡都有自己的控制器使用 ng-controller。这是我收到错误的地方:[$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- tabController 这是预料之中的,因为我们似乎不能在 uibModalInstance 中包含 ng-controller。 是否有其他方法可以实现此目的?

它应该对你有用 |- demo fiddle - |- demo plnkr with template files -|我已经为你创造了。请仔细比较此解决方案与您的解决方案。您可以根据需要在 uibModal 模板中定义任意数量的 ng-controller。还要确保您的控制器是同一模块的一部分。

查看

 <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申请

 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');
    };
});