无法使用bootstrap打开angularjs中的模态窗口

问题描述 投票:2回答:2

这是我的app.js文件:

const app = angular.module('CurseTransport', [
    'ui.router',
    'ui.bootstrap',
    'ngMessages',
    'raceModule',


])

app.config(['$stateProvider', '$urlRouterProvider', '$qProvider', function($stateProvider, $urlRouterProvider, $qProvider) {
    $qProvider.errorOnUnhandledRejections(false)
    $urlRouterProvider.otherwise('/races')
    $stateProvider
        .state('races', {
            url : '/races',
            templateUrl :'views/races.html',
            controller:'racesController'
        })
          .state('racesInsert', {
            url: '/races/insert',
            onEnter: ['$stateParams', '$state', '$modal',
              function($stateParams, $state, $modal) {
                $modal

                  // handle modal open
                  .open({
                    template: 'views/racesInsert',
                    windowClass : 'show',
                    controller: ['$scope',
                      function($scope) {
                        // handle after clicking Cancel button
                        $scope.cancel = function() {
                          $scope.$dismiss();
                        };
                        // close modal after clicking OK button
                        $scope.ok = function() {
                          $scope.$close(true);
                        };
                      }
                    ]
                  })

                  // change route after modal result
                  .result.then(function() {
                    // change route after clicking OK button
                    $state.transitionTo('races');
                  }, function() {
                    // change route after clicking Cancel button or clicking background
                    $state.transitionTo('races');
                  });

              }
            ]

          });





}])

我对模态窗口的看法:

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

当我单击我的按钮打开模态窗口时,它不起作用。我的控制台没有错误。我也包含在我的脚本中。

我的模态位于不同的html文件中。

javascript angularjs twitter-bootstrap modal-dialog
2个回答
1
投票

你看过文件了吗? https://angular-ui.github.io/bootstrap/#/modal

在引用模态模板时,您需要使用templateUrl,而不是template,并确保包含文件扩展名:

$modal.open({
  templateUrl: 'views/racesInsert.html',
  windowClass : 'show',
  ...

如果您像这样定义内联模板,请仅使用template

$modal.open({
  template: '<div class="modal-header"><h1>Modal Heading</h1></div>...',
  windowClass: 'show',
  ...

如果您使用内联模板作为脚本,则需要使用正确的脚本标记声明模板。并且,不要包含外部对话框容器:

<script id="racesInsert.html" type="text/ng-template">
  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Modal Header</h4>
    </div>
    <div class="modal-body">
      <p>Some text in the modal.</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
</script>

$modal.open({
  templateUrl: 'racesInsert.html',
  windowClass : 'show',
  ...

您没有提到您正在使用哪个版本的Angular UI Bootstrap。当前版本使用$uibModal而不是$modal作为导入的模块。


0
投票

这是你需要调用模态的js函数 -

$scope.functionName=function(){
 var uibModalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'html page path',
        controller: 'controllerName',
        size: 'lg',
        resolve: {
            deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load({
                    name: 'app Name',
                    insertBefore: '#ng_load_plugins_before',
                    files: ['js controller file path']
                });
            }]
        }
    });
    uibModalInstance.result.then(function (selectedItem) {
        $scope.selected = selectedItem;
    }, function () {

     });
}

需要打开弹出窗口时调用函数functionName。

您需要在当前控制器中添加$ uibModal作为依赖项,模型控制器应该将$ uibModalInstance作为依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.