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

时间:2017-11-23 16:30:12

标签: javascript angularjs twitter-bootstrap angular-ui-bootstrap

我正在尝试使用引导程序modal,并且我正在$uibModal.open({controller : "mainCtrl"})中传递主控制器。

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

1 个答案:

答案 0 :(得分:1)

它应该适合你 - | 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');
    };
});