【问题标题】:Can't open a modal window in angularjs, using bootstrap使用引导程序无法在angularjs中打开模态窗口
【发布时间】:2017-01-04 16:26:14
【问题描述】:

这是我的 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 文件中。

【问题讨论】:

  • 我认为你必须调用 open 方法 .. 和一个问题 .. qhy 你在路由中声明它? .. 在racesController 中贴花然后在你的racesController 的init() 方法中调用它(打开)不是更好吗?

标签: javascript angularjs twitter-bootstrap modal-dialog


【解决方案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作为导入模块。

【讨论】:

    【解决方案2】:

    这里是modal需要调用的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 作为依赖项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2018-01-04
      • 2013-09-27
      相关资源
      最近更新 更多