答案 0 :(得分:1)
根据我的评论,这是一个解决方案:
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
}
});
})