AngularJS open modal on button click

后端 未结 4 909
青春惊慌失措
青春惊慌失措 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:20

    Hope this will help you .

    Here is Html code:-

        
        

    Modal example

    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: '',
          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

提交回复
热议问题