【问题标题】:Bootstrap uib modal with multiple controllers具有多个控制器的引导 uib 模式
【发布时间】:2018-05-07 16:18:08
【问题描述】:

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

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

【问题讨论】:

  • 您可以在 uibModal 模板中定义任意数量的ng-controller。问题出在其他地方。
  • 这是一个已知问题 - stackoverflow.com/questions/33930325/… 所有答案都要求您删除 ng-controller。
  • 这是不正确的。您可以拥有任意数量的ng-controller。请检查我的答案。

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


【解决方案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');
    };
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 2015-08-02
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多