我需要编写一个自定义指令,以便在单击按钮(DOM的空白区域)时关闭(或隐藏)按钮。换句话说除了按钮之外的任何地方?当我应用自定义指令时,此行为应仅适用于此按钮。任何指南都将受到高度赞赏。
<button class="btn btn-primary" ng-click="CreateUpdate()">Submit</button>
app.directive('MyDirective', function() {
//content
});
答案 0 :(得分:0)
在按钮指令中,您可以注入$ document。然后点击文档点击。
<button class="myButton btn btn-primary" ng--lick="CreateUpdate()">Submit</button>
mainModule.directive("myButton", ['$document',
function ($document) {
return {
restrict: "E",
link: function (scope, element, attrs) {
$document.bind('click', function(event){
//Get the element clicked
var clickedElement = angular.element(event.target);
// If the clickedElement is not same as button (say, the clicked element class is different from button class) then close the button.
}
}
})