代码之家  ›  专栏  ›  技术社区  ›  Nikhil

具有多个控制器的引导uib模式

  •  1
  • Nikhil  · 技术社区  · 8 年前

    我正在尝试使用引导 modal 我正在通过主控制器 $uibModal.open({controller : "mainCtrl"}) .

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

    1 回复  |  直到 8 年前
        1
  •  1
  •   lin    8 年前

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

    看法

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