最佳答案
这是根据我的评论的解决方案:
http://plnkr.co/edit/2UG1Kj2l3fuVgYXaOB6d?p=preview
我有一个自定义指令位于主体
<body class="container" ng-controller="mainCtrl" dropdown-listener>
它监听窗口的点击事件。
它会触发您的 dropdownDirective 正在监听的事件:
myApp.directive('dropdownListener', function ($window, $rootScope) {
return {
restrict: 'A',
link: function(scope, element, attr) {
var w = angular.element($window);
w.bind('click', function(){
$rootScope.$broadcast('dropdown:close');
});
}
}
});
在您的dropdownDirective
$scope.$on('dropdown:close', function (event, data) {
$scope.$apply(function() {
if($scope.open) { //only close when it is open
$scope.open = !$scope.open; //quick and dirty solution - withouth apply it didn't work. Maybe you could investigate further
}
});
})
关于javascript - 点击退出后下拉菜单不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721900/