0

I have a custom popup panel that I want to add functionality to close whenever the screen is clicked outside the panel. I created a directive to accomplish this, but I am having a hard time communicating that the close event happened.

<div ng-if="ui.showConfig" auto-hide="ui.showConfig=false">...</div>

JS

myApp.directive('autoHide',
    [function() {
        return {
            restrict: 'A',
            scope: {
              'autoHide': '&'
             },
            link: function (scope, element, attrs) {
                var $overlay = $("<div style='position:absolute;top:0;left:0;width:100%;height:100%;z-index:2000;'>");
                $overlay
                    .appendTo("body")
                    .one("click", function(e){ 
                        element.hide();
                        $overlay.unbind().remove(); 
                        // This is not firing...
                        // scope.autoHide(); 
                    });

                element.css("zIndex", "2001");

                element.on('$destroy', function() {
                    $overlay.unbind().remove();
                });
            }
        };
    }]);

Here is a jsfiddle

The overlay click event is firing. The panel is hiding. But ui.showConfig is not getting set to false causing the user to click the button twice to open up the panel next time.

Also, if there is another approach I should consider please let me know!

1
  • Can you post your code in plunker? Commented Aug 28, 2014 at 19:09

3 Answers 3

1

Inside an event handler you need to call scope.$apply() so that angular gets informed about changes:

// This is not firing...
scope.autoHide(); 
scope.$apply();
Sign up to request clarification or add additional context in comments.

Comments

0

i did try your jsfiddle and http://jsfiddle.net/yke1pm7g/1/ apparently the event fires, however i don't understand whats going on with scope.autoHide() ?

Comments

0

I want to suggest you at the first do not use the jquery appending in DOM extra elements or something else, and also I want to suggest use already build angular bootstrap modal which will gave you more advantages. You can check the link and see the examples and how to include the module in your project. I'm pretty sure what the all functionality you need for popup bootstrap mpodal already has.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.