AngularJS open modal on button click

后端 未结 4 874
青春惊慌失措
青春惊慌失措 2021-01-05 04:37

I am trying to learn to open a modal dialog box at the click of the button in AngularJS but unable to do so. I checked the chrome console but there are no errors. Also, sinc

相关标签:
4条回答
  • 2021-01-05 05:06

    Set Jquery in scope

    $scope.$ = $;

    and call in html

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

    0 讨论(0)
  • 2021-01-05 05:07

    You should take a look at Batarang for AngularJS debugging

    As for your issue:

    Your scope variable is not directly attached to the modal correctly. Below is the adjusted code. You need to specify when the modal shows using 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 -->
    
    0 讨论(0)
  • 2021-01-05 05:15

    I am not sure,how you are opening popup or say model in your code. But you can try something like this..

    <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>
    
    0 讨论(0)
  • 2021-01-05 05:20

    Hope this will help you .

    Here is Html code:-

        <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 code:-

    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;
              });
            });
          }
        };
      });
    

    Check this--jsfiddle

    0 讨论(0)
提交回复
热议问题