模态表单未在点击时打开

时间:2017-04-21 07:41:08

标签: javascript html angularjs bootstrap-modal modalviewcontroller

我有以下角度html,试图在点击按钮时打开模态表单。代码创建按钮就好了,但点击它不会做任何事情。

为什么按钮没有打开表单?

var myMod = angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function() {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        items: function() {
          return $scope.items;
        }
      }
    });

    modalInstance.result.then(function(selectedItem) {
      $scope.selected = selectedItem;
    }, function() {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
};

var ModalInstanceCtrl = function($scope, $modalInstance, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function() {
    $modalInstance.close($scope.selected.item);
  };

  $scope.cancel = function() {
    $modalInstance.dismiss('cancel');
  };
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="ModalDemoCtrl">
  <script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
      <h3>I'm a modal!</h3>
    </div>
    <div class="modal-body">
      <ul>
        <li ng-repeat="item in items">
          <a ng-click="selected.item = item">{{ item }}</a>
        </li>
      </ul>
      Selected: <b>{{ selected.item }}</b>
    </div>
    <div class="modal-footer">
      <button class="btn btn-primary" ng-click="ok()">OK</button>
      <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
  </script>
  <h1>GWAT Websites and Designs</h1>
  <button class="btn" ng-click="open()">Submit new post</button>
</div>

1 个答案:

答案 0 :(得分:2)

我做了这些改变:

  1. 包括<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
  2. 添加ng-app="plunker"
  3. 注册控制器:myMod.controller('ModalDemoCtrl', ModalDemoCtrl); myMod.controller('ModalInstanceCtrl', ModalInstanceCtrl);
  4. 添加<p>Selected: {{selected}}</p>以查看模态
  5. 中的所选项目
  6. $modal更改为$uibModal,将$modalInstance更改为$uibModalInstance
  7. var myMod = angular.module('plunker', ['ui.bootstrap']);
    var ModalDemoCtrl = function($scope, $uibModal, $log) {
      $scope.items = ['item1', 'item2', 'item3'];
    
      $scope.open = function() {
        var modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          controller: ModalInstanceCtrl,
          resolve: {
            items: function() {
              return $scope.items;
            }
          }
        });
    
        modalInstance.result.then(function(selectedItem) {
          $scope.selected = selectedItem;
        }, function() {
          $log.info('Modal dismissed at: ' + new Date());
        });
      };
    };
    
    var ModalInstanceCtrl = function($scope, $uibModalInstance, items) { 
      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };
    
      $scope.ok = function() {
        $uibModalInstance.close($scope.selected.item);
      };
    
      $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
      };
    };
    
    myMod.controller('ModalDemoCtrl', ModalDemoCtrl);
    myMod.controller('ModalInstanceCtrl', ModalInstanceCtrl);
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    
    <div ng-app='plunker' ng-controller="ModalDemoCtrl">
      <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
          <h3>I'm a modal!</h3>
        </div>
        <div class="modal-body">
          <ul>
            <li ng-repeat="item in items">
              <a ng-click="selected.item = item">{{ item }}</a>
            </li>
          </ul>
          Selected: <b>{{ selected.item }}</b>
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary" ng-click="ok()">OK</button>
          <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
      </script>
      <h1>GWAT Websites and Designs</h1>
      <p>Selected: {{selected}}</p>
      <button class="btn" ng-click="open()">Submit new post</button>
    </div>