gpt4 book ai didi

javascript - 如果在 angularjs 中的 div 外部单击,如何隐藏 div

转载 作者:行者123 更新时间:2023-11-28 04:19:18 25 4
gpt4 key购买 nike

For the notifications I want that my div will be hidden if clicked outside of it plunker code link the html code is

<div ng-hide="notification" ng-click="notifications($event);"><i class="fa fa-bell" aria-hidden="true"></i></div>
<div ng-hide="notifyData">
<ul>
<li ng-repeat="noti in newNotificationarray">
<div class="notifymsg">{{noti.msg}}</div>
<div class="notifytitle">
<a href="#">{{noti.title}}</a>
</div>
<div class="notifyName">{{noti.cName}}</div>
</li>
</ul>
</div>

the js code in controller is

$scope.notifications=function(event){
$scope.notifyData=!($scope.notifyData);
event.stopPropagation();
};

window.onclick = function(){
if ($scope.notifyData) {
$scope.notifyData=false;
$scope.$apply();
}
};

If I do it without using "event.stopPropagation();" then the div hide when clicked anywhere on the screen and also on the notification div

最佳答案

首先,您需要注入(inject)窗口服务。您应该这样做,以便可以在您的测试中模拟它。

此外,更新 window 的 onclick 处理程序中的逻辑。

var app = angular.module("myapp", []);
app.controller('mycontroller', ['$scope', '$window', function($scope, $window) {
...
$window.onclick = function(event) {
if (!$scope.notifyData) {
$scope.notifyData = true;
$scope.$apply();
}
}

}]);

关于javascript - 如果在 angularjs 中的 div 外部单击,如何隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45575731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com