Bootstrap alert message represented as modal, angular

前端 未结 3 791
猫巷女王i
猫巷女王i 2021-02-02 04:31

Bootstrap 3 provides Bootstrap: event messages: success, info, warning, danger.

However sometimes the view doesn\'t have enough space to show up

3条回答
  •  被撕碎了的回忆
    2021-02-02 05:08

    I'll answer on my own question.

    Simple way

    The flow is pretty simple and straightforward. We don't reinvent the wheel here.

    We don't need nor header neither footer:

    Dialog template HTML:

    
    

    We even don't need to use ng-class:

    class="alert-{{data.mode}}"
    

    where mode might be: success, info, warning, danger


    Modal Instance Controller:

    var ModalInstanceCtrl = function ($scope, $modalInstance, data) {
      $scope.data = data;
      $scope.close = function(/*result*/){
        $modalInstance.close($scope.data);
      };
    };
    

    And this is modal configuration and content:

     $scope.data = {
        boldTextTitle: "Done",
        textAlert : "Some content",
        mode : 'info'
      }  
    
    var modalInstance = $modal.open({
          templateUrl: 'myModalContent.html',
          controller: ModalInstanceCtrl,
          backdrop: true,
          keyboard: true,
          backdropClick: true,
          size: 'lg',
          resolve: {
            data: function () {
              return $scope.data;
            }
          }
        });
    

    Demo Plunker


    enter image description here

    Directive way

    Demo 2 Plunker

    We can put all above written code into directive for better maintenance:

    HTML

    
    

    Directive

    .directive('myAlert', function($modal,$log) {
          return {
            restrict: 'E',
            scope: {
              mode: '@',
              boldTextTitle: '@',
              textAlert : '@'
            },
            link: function(scope, elm, attrs) {
            
           scope.data= {
                    mode:scope.mode,
                    boldTextTitle:scope.boldTextTitle,
                    textAlert:scope.textAlert
                  }
            
           var ModalInstanceCtrl = function ($scope, $modalInstance, data) {
              
               console.log(data);
              
               scope.data= {
                  mode:scope.mode || 'info',
                  boldTextTitle:scope.boldTextTitle || 'title',
                  textAlert:scope.textAlert || 'text'
              }
            };
            
            elm.parent().bind("click", function(e){
               scope.open();
           });
            
         scope.open = function () {
            
            var modalInstance = $modal.open({
              templateUrl: 'myModalContent.html',
              controller: ModalInstanceCtrl,
              backdrop: true,
              keyboard: true,
              backdropClick: true,
              size: 'lg',
              resolve: {
                data: function () {
                  return scope.data;
                }
              }
            });
        
        
            modalInstance.result.then(function (selectedItem) {
              scope.selected = selectedItem;
            }, function () {
              $log.info('Modal dismissed at: ' + new Date());
            });
        }
      }
      };
    })
    

    Hope it will save time to someone.

提交回复
热议问题