javascript - 如何将 Bootstrap 模式窗口与 angularjs 一起使用?

标签 javascript angularjs twitter-bootstrap

我想使用 bootstrap 弹出窗口进行一些用户确认,但我最初通过将 bootstrap 与 angularjs 一起使用而陷入困境,知道下面实现了什么错误以拥有 bootstrap 模态窗口或使用 angularjs 的 bootstrap 模态窗口的更好方法吗?我在控制台中没有看到任何错误。

app.js

angular.module('App', [
    'ui.router',
    'ui.bootstrap']).config(function ($stateProvider, $httpProvider, $urlRouterProvider) {

//config code here ..
});

ctrl.js

angular.module('App').controller('modalController',function($scope,$uibModal){
    $scope.openModal = function () {
            console.log('opening pop up');
            var modalInstance = $uibModal.open({
                templateUrl: 'web/global/modal.html',
            });
        }
});

home.html

<button class="btn btn-warning" ng-click="openModal()">Simple Popup</button>

最佳答案

ANGULAR UI BOOTSTRAP :


工作示例:

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function($scope, $modal, $log) {

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

  $scope.animationsEnabled = true;

  $scope.open = function(size) {

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

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

  $scope.toggleAnimation = function() {
    $scope.animationsEnabled = !$scope.animationsEnabled;
  };

});

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

angular.module('ui.bootstrap.demo').controller('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');
  };
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.3.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="ModalDemoCtrl">
    <script type="text/ng-template" id="myModalContent.html">
      <div class="modal-header">
        <h3 class="modal-title">I'm a modal!</h3>
      </div>
      <div class="modal-body">
        <ul>
          <li ng-repeat="item in items">
            <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
          </li>
        </ul>
        Selected: <b>{{ selected.item }}</b>
      </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>
    <button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
    <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
    <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>
    <div ng-show="selected">Selection from a modal: {{ selected }}</div>
  </div>
</body>

</html>

关于javascript - 如何将 Bootstrap 模式窗口与 angularjs 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575595/

相关文章:

twitter-bootstrap - 在 Bootstrap 中使用 Font Awesome 而不覆盖字形

javascript - Angular js的单个复选框

javascript - 如果数组中存在匹配文本,则突出显示页面上的文本

javascript - 在 html 列表中查找第 n 个元素列表项的最佳方法?

javascript - 获取 <h1> 的内容将其转换为文本并设置为页面标题

javascript - JQuery:根据浏览器大小调整图像大小

Angularjs:将数据从一个工厂调用存储到变量中,以便在另一个工厂调用中使用

angularjs - 相关和嵌套资源的 REST Api 端点设计

javascript - if 语句中的函数仅在第一次时未定义,但在休息时间函数调用也未定义

html - 在 Bootstrap 网格内垂直居中 div