我需要在点击它外部时隐藏一个 div。所以,我创建了一个处理它的指令(检查目标元素是否是子元素。如果是,它不会隐藏 div,否则它会隐藏 div)。当我们从预先输入的下拉菜单中选择某个选项时,它失败了(使 div 隐藏)。有什么帮助吗?
http://plnkr.co/edit/hP740WSct8BuZLm8K1K9?p=preview
指示:
app.directive("outsideClick", ['$document', '$parse', function($document, $parse) {
return {
link: function($scope, $element, $attributes) {
var scopeExpression = $attributes.outsideClick,
onDocumentClick = function(event) {
var parent = event.target;
while (parent && parent !== $element[0]) {
parent = parent.parentNode;
}
if (!parent) {
$scope.$apply(scopeExpression);
}
}
$document.on("click", onDocumentClick);
$element.on('$destroy', function() {
$document.off("click", onDocumentClick);
});
}
}
}]);
HTML:
<div ng-show="status" outside-click="hideDiv();">
<h3>Div to be made hidden</h3>
<p>Selected: {{address.selected.formatted_address}}</p>
<ui-select ng-model="address.selected" theme="bootstrap" ng-disabled="disabled" reset-search-input="false" style="width: 300px;">
<ui-select-match placeholder="Enter an address...">{{$select.selected.formatted_address}}</ui-select-match>
<ui-select-choices repeat="address in addresses track by $index" refresh="refreshAddresses($select.search)" refresh-delay="0">
<div ng-bind-html="address.formatted_address | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
</div>