На мой взгляд, ng-blur
не очень хорошая идея для этого случая, и @isherwood прав !, но мы можем использовать пользовательские директивы, такие как clickOff
, для случая, когда нам нужно вызвать действие клика за пределами нашей цели, я надеюсь, что это полезно дляты мой друг.
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
$scope.outsideTable = function(){
console.log("outside table")
}
$scope.insideTable = function(){
console.log("inside table")
}
})
app.directive("clickOff", ["$parse", "$document", function ($parse, $document) {
var dir = {
compile: function ($element, attr) {
// Parse the expression to be executed
// whenever someone clicks _off_ this element.
var fn = $parse(attr["clickOff"]);
return function (scope, element, attr) {
// add a click handler to the element that
// stops the event propagation.
element.bind("click", function (event) {
event.stopPropagation();
});
angular.element($document[0].body).bind("click", function (event) {
scope.$apply(function () {
fn(scope, { $event: event });
});
});
};
}
};
return dir;
}]);
main {
height: 100vh;
background: #eee;
}
table {
width: 100%;
}
table tr td, table tr th {
border: solid 1px #ccc;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<main ng-app="app" ng-controller="ctrl">
<table click-off="outsideTable()" ng-click="insideTable()">
<tr>
<th>Table</th>
</tr>
<tr>
<td>ngMouseLeave custom directive</td>
</tr>
</table>
</main>