【问题标题】:AngularJS open modal on button clickAngularJS在按钮单击时打开模式
【发布时间】:2015-07-07 10:09:17
【问题描述】:

我正在尝试学习在 AngularJS 中单击按钮时打开模式对话框,但无法这样做。我检查了 chrome 控制台,但没有错误。另外,由于我正在学习AngularJS,请建议当chrome控制台没有显示任何错误时该怎么做。

这是我的代码

<html ng-app="MyApp">
<head>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" />

<script type="text/javascript">
    var myApp = angular.module("MyApp", []);
    myApp.controller('MyController', function ($scope) {

        $scope.open = function () {
            $scope.showModal = true;
        };

        $scope.ok = function () {
            $scope.showModal = false;
        };

        $scope.cancel = function () {
            $scope.showModal = false;
        };
    });
</script>
</head>
<body ng-controller="MyController">

    <button class="btn btn-primary" ng-click="open()">Test Modal</button>

    <!-- Confirmation Dialog -->
    <div class="modal" modal="showModal" close="cancel()">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title">Delete confirmation</h4>
          </div>
          <div class="modal-body">
            <p>Are you sure?</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="cancel()">No</button>
            <button type="button" class="btn btn-primary" ng-click="ok()">Yes</button>
          </div>
        </div>
      </div>
    </div>
    <!-- End of Confirmation Dialog -->

 </body>
 </html>

【问题讨论】:

  • 因为您使用的是 Angular 和 Bootstrap,所以我会在这里使用模态形式:angular-ui.github.io/bootstrap
  • 这里你有一个简单的example 如何使用 angularjs 的模态

标签: angularjs


【解决方案1】:

在范围内设置 Jquery

$scope.$ = $;

并在 html 中调用

ng-click="$('#novoModelo').modal('show')"

【讨论】:

    【解决方案2】:

    希望这会对你有所帮助。

    这里是 Html 代码:-

        <body>
        <div ng-controller="MyController" class="container">
          <h1>Modal example</h1>
          <button ng-click="open()" class="btn btn-primary">Test Modal</button>
    
          <modal title="Login form" visible="showModal">
            <form role="form">
    
            </form>
          </modal>
        </div>
    </body>
    

    AngularJs 代码:-

    var mymodal = angular.module('mymodal', []);
    
    mymodal.controller('MyController', function ($scope) {
        $scope.showModal = false;
        $scope.open = function(){
        $scope.showModal = !$scope.showModal;
        };
      });
    
    mymodal.directive('modal', function () {
        return {
          template: '<div class="modal fade">' + 
              '<div class="modal-dialog">' + 
                '<div class="modal-content">' + 
                  '<div class="modal-header">' + 
                    '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' + 
                    '<h4 class="modal-title">{{ title }}</h4>' + 
                  '</div>' + 
                  '<div class="modal-body" ng-transclude></div>' + 
                '</div>' + 
              '</div>' + 
            '</div>',
          restrict: 'E',
          transclude: true,
          replace:true,
          scope:true,
          link: function postLink(scope, element, attrs) {
            scope.title = attrs.title;
    
            scope.$watch(attrs.visible, function(value){
              if(value == true)
                $(element).modal('show');
              else
                $(element).modal('hide');
            });
    
            $(element).on('shown.bs.modal', function(){
              scope.$apply(function(){
                scope.$parent[attrs.visible] = true;
              });
            });
    
            $(element).on('hidden.bs.modal', function(){
              scope.$apply(function(){
                scope.$parent[attrs.visible] = false;
              });
            });
          }
        };
      });
    

    检查这个--jsfiddle

    【讨论】:

    • 非常好。谢谢。
    • 当我点击“测试模式”时,什么也没有发生。非模态出现并且控制台日志中没有错误。其他人也会出现这种情况吗?
    • @truffle 在 Safari 中没有任何反应
    • @Tamara,当我使用它时它工作正常,但现在我面临同样的问题,可能只是因为动画而发生。所以尝试禁用模态上的动画。
    • 查看此示例,希望对您有所帮助:) plnkr.co/edit/kQz0fiaXLv7T37N8fzJU?p=preview
    【解决方案3】:

    我不确定,您是如何在代码中打开弹出窗口或说出模型的。 但是你可以试试这样的..

    <html ng-app="MyApp">
    <head>
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    
    <script type="text/javascript">
        var myApp = angular.module("MyApp", []);
        myApp.controller('MyController', function ($scope) {
          $scope.open = function(){
            var modalInstance = $modal.open({
                            templateUrl: '/assets/yourOpupTemplatename.html',
                            backdrop:'static',
                            keyboard:false,
                            controller: function($scope, $modalInstance) {
                                $scope.cancel = function() {
                                    $modalInstance.dismiss('cancel');
                                };
                                $scope.ok = function () {
                                  $modalInstance.close();
                                };
                            }
                        });
          }
        });
    </script>
    </head>
    <body ng-controller="MyController">
    
        <button class="btn btn-primary" ng-click="open()">Test Modal</button>
    
        <!-- Confirmation Dialog -->
        <div class="modal">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Delete confirmation</h4>
              </div>
              <div class="modal-body">
                <p>Are you sure?</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" ng-click="cancel()">No</button>
                <button type="button" class="btn btn-primary" ng-click="ok()">Yes</button>
              </div>
            </div>
          </div>
        </div>
        <!-- End of Confirmation Dialog -->
    
     </body>
     </html>
    

    【讨论】:

    【解决方案4】:

    你应该看看 Batarang 进行 AngularJS 调试

    至于你的问题:

    您的范围变量没有正确地直接附加到模态。下面是调整后的代码。您需要使用ng-show指定模态显示的时间

    <!-- Confirmation Dialog -->
    <div class="modal" modal="showModal" ng-show="showModal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title">Delete confirmation</h4>
          </div>
          <div class="modal-body">
            <p>Are you sure?</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="cancel()">No</button>
            <button type="button" class="btn btn-primary" ng-click="ok()">Yes</button>
          </div>
        </div>
      </div>
    </div>
    <!-- End of Confirmation Dialog -->
    

    【讨论】:

    • 抱歉,这段代码似乎不起作用。我们还遗漏了什么吗?
    • 是的,将 showModal 声明为 $scope 函数调用之外的变量
    猜你喜欢
    • 2023-03-15
    • 2017-12-12
    • 2014-08-14
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多