Search code examples
angularjspopup

Simple Popup by using Angular JS


I am new to using AngularJS. I have 4 buttons named Cancel, Deny, Success & Remove. If am I click on any button I want to show multiple message i.e. If I click on Cancel then message showing you click on Cancel button & so on.

I have follow from below link but simple alert & no controller is used in this example.

I want a A MODAL POPUP IN AN ANGULARJS DIRECTIVE example so I can understand. I have try to google it but no simple examples found.

Please help. Thanks

<!doctype html>
<html ng-app="mymodal">


<body>

<div ng-controller="MainCtrl" class="container">
  <button ng-click="toggleModal('Success')" class="btn btn-default">Success</button>
  <button ng-click="toggleModal('Remove')" class="btn btn-default">Remove</button>
  <button ng-click="toggleModal('Deny')" class="btn btn-default">Deny</button>
  <button ng-click="toggleModal('Cancel')" class="btn btn-default">Cancel</button>
  <modal visible="showModal">
      Any additional data / buttons
  </modal>
</div>

        <!-- Scripts -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"></script>


    <!-- App -->
    <script>
        var mymodal = angular.module('mymodal', []);

mymodal.controller('MainCtrl', function ($scope) {
    $scope.showModal = false;
    $scope.buttonClicked = "";
    $scope.toggleModal = function(btnClicked){
        $scope.buttonClicked = btnClicked;
        $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">{{ buttonClicked }} clicked!!</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.$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;
          });
        });
      }
    };
  });




    </script>
</body>
</html>

Link : http://plnkr.co/edit/?p=preview

Solution

  • Built a modal popup example using syarul's jsFiddle link. Here is the updated fiddle.

    Created an angular directive called modal and used in html. Explanation:-

    HTML

    <div ng-controller="MainCtrl" class="container">
      <button ng-click="toggleModal('Success')" class="btn btn-default">Success</button>
      <button ng-click="toggleModal('Remove')" class="btn btn-default">Remove</button>
      <button ng-click="toggleModal('Deny')" class="btn btn-default">Deny</button>
      <button ng-click="toggleModal('Cancel')" class="btn btn-default">Cancel</button>
      <modal visible="showModal">
          Any additional data / buttons
      </modal>
    </div>
    

    On button click toggleModal() function is called with the button message as parameter. This function toggles the visibility of popup. Any tags that you put inside will show up in the popup as content since ng-transclude is placed on modal-body in the directive template.

    JS

    var mymodal = angular.module('mymodal', []);
    
    mymodal.controller('MainCtrl', function ($scope) {
        $scope.showModal = false;
        $scope.buttonClicked = "";
        $scope.toggleModal = function(btnClicked){
            $scope.buttonClicked = btnClicked;
            $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">{{ buttonClicked }} clicked!!</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;
              });
            });
          }
        };
      });
    

    UPDATE

    <!doctype html>
    <html ng-app="mymodal">
    
    
    <body>
    
    <div ng-controller="MainCtrl" class="container">
      <button ng-click="toggleModal('Success')" class="btn btn-default">Success</button>
      <button ng-click="toggleModal('Remove')" class="btn btn-default">Remove</button>
      <button ng-click="toggleModal('Deny')" class="btn btn-default">Deny</button>
      <button ng-click="toggleModal('Cancel')" class="btn btn-default">Cancel</button>
      <modal visible="showModal">
          Any additional data / buttons
      </modal>
    </div>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
            <!-- Scripts -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    
        <!-- App -->
        <script>
            var mymodal = angular.module('mymodal', []);
    
    mymodal.controller('MainCtrl', function ($scope) {
        $scope.showModal = false;
        $scope.buttonClicked = "";
        $scope.toggleModal = function(btnClicked){
            $scope.buttonClicked = btnClicked;
            $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">{{ buttonClicked }} clicked!!</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.$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;
              });
            });
          }
        };
      });
    
        </script>
    </body>
    </html>
    

    UPDATE 2 restrict : 'E' : directive to be used as an HTML tag (element). Example in our case is

    <modal>
    

    Other values are 'A' for attribute

    <div modal>
    

    'C' for class (not preferable in our case because modal is already a class in bootstrap.css)

    <div class="modal">