ups the most elegant way

I have this AngularJS app. Everything works just fine.

Now I need to show different pop-ups when specific conditions become true, and I was wondering what would be the best way to proceed.

Currently I'm evaluating two options, but I'm absolutely open to other options.


Option 1

I could create the new HTML element for the pop-up, and append to the DOM directly from the controller.

This will break the MVC design pattern. I'm not happy with this solution.


Option 2

I could always insert the code for all the pop-ups in the static HTML file. Then, using ngShow , I can hide / show only the correct pop-up.

This option is not really scalable.


So I'm pretty sure there has to be a better way to achieve what I want.


Based on my experience with AngularJS modals so far I believe that the most elegant approach is a dedicated service to which we can provide a partial (HTML) template to be displayed in a modal.

When we think about it modals are kind of AngularJS routes but just displayed in modal popup.

The AngularUI bootstrap project (http://angular-ui.github.com/bootstrap/) has an excellent $modal service (used to be called $dialog prior to version 0.6.0) that is an implementation of a service to display partial's content as a modal popup.


It's funny because I'm learning Angular myself and was watching some video's from their channel on Youtube. The speaker mentions your exact problem in this video https://www.youtube.com/watch?v=ZhfUv0spHCY#t=1681 around the 28:30 minute mark.

It comes down to placing that particular piece of code in a service rather then a controller.

My guess would be to inject new popup elements into the DOM and handle them separate instead of showing and hiding the same element. This way you can have multiple popups.

The whole video is very interesting to watch as well :-)


  • Create a 'popup' directive and apply it to the container of the popup content
  • In the directive, wrap the content in a absolute position div along with the mask div below it.
  • It is OK to move the 2 divs in the DOM tree as needed from within the directive. Any UI code is OK in the directives, including the code to position the popup in center of screen.
  • Create and bind a boolean flag to controller. This flag will control visibility.
  • Create scope variables that bond to OK / Cancel functions etc.
  • Editing to add a high level example (non functional)

    <div id='popup1-content' popup='showPopup1'>
      ....
      ....
    </div>
    
    
    <div id='popup2-content' popup='showPopup2'>
      ....
      ....
    </div>
    
    
    
    .directive('popup', function() {
      var p = {
          link : function(scope, iElement, iAttrs){
               //code to wrap the div (iElement) with a abs pos div (parentDiv)
              // code to add a mask layer div behind 
              // if the parent is already there, then skip adding it again.
             //use jquery ui to make it dragable etc.
              scope.watch(showPopup, function(newVal, oldVal){
                   if(newVal === true){
                       $(parentDiv).show();
                     } 
                  else{
                     $(parentDiv).hide();
                    }
              });
          }
    
    
       }
      return p;
    });
    
    链接地址: http://www.djcxy.com/p/88174.html

    上一篇: 为什么要使用表格来进行网站布局这样的恶魔

    下一篇: 最优雅的方式