AngularJS dropdown directive hide when clicking outside

后端 未结 9 2131
长发绾君心
长发绾君心 2020-11-29 22:06

I\'m trying to create a multiselect dropdown list with checkbox and filter option. I\'m trying to get the list hidden with I click outside but could not figure it out how. A

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 23:04

    This is an old post but in case this helps anyone here is a working example of click outside that doesn't rely on anything but angular.

    module('clickOutside', []).directive('clickOutside', function ($document) {
    
            return {
               restrict: 'A',
               scope: {
                   clickOutside: '&'
               },
               link: function (scope, el, attr) {
    
                   $document.on('click', function (e) {
                       if (el !== e.target && !el[0].contains(e.target)) {
                            scope.$apply(function () {
                                scope.$eval(scope.clickOutside);
                            });
                        }
                   });
               }
            }
    
        });
    

提交回复
热议问题