gpt4 book ai didi

javascript - AngularJS - 停止传播

转载 作者:行者123 更新时间:2023-11-30 18:03:27 32 4
gpt4 key购买 nike

我真的很难实现 stopPropagation。我目前正在使用多个下拉菜单。我想对其进行设置,以便当您打开一个菜单,然后单击它以外的任何地方时,该菜单将关闭。理想情况下一次只能打开一个菜单,因此当您打开一个菜单然后单击另一个菜单时,第一个菜单会关闭。

也许我正在以一种完全错误的方式来处理这个问题。如果是这样,请向正确的方向点头表示感谢!

谢谢!

下面是我如何处理下拉菜单的打开和关闭:

<a ng-click="popout.isVisible = !popout.isVisible">Drop Menu #1</a>
<ul ng-show="popout.isVisible">
<li>This is some text.</li>
<li>This is some text.</li>
<li>This is some text.</li>
</ul>

<a ng-click="popout.isVisible = !popout.isVisible">Drop Menu #2</a>
<ul ng-show="popout.isVisible">
<li>This is some text.</li>
<li>This is some text.</li>
<li>This is some text.</li>
</ul>

这是我发现的一些代码,它们创建了一个 stopEvent 指令,但没有按照我需要的方式工作:

myApp.directive('stopEvent', function () {
return {
link: function (scope, element, attr) {
element.bind(attr.stopEvent, function (e) {
e.stopPropagation();
alert('ALERT!');
});
}
};
});

最佳答案

您在那里引用了 stopEvent 指令,这对我来说看起来不错,但实际上您并没有在 HTML 中使用它。这是您所描述内容的极其基本的实现:

http://jsfiddle.net/KzfSM/1/

HTML

<div ng-app="app" ng-controller="p" ng-click="hideEverything()">

<a ng-click="popout[0].isVisible = !popout[0].isVisible" stop-event="click">Drop Menu #1</a>
<ul ng-show="popout[0].isVisible" stop-event="click">
<li>1111</li>
<li>This is some text.</li>
<li>This is some text.</li>
</ul>

<a ng-click="popout[1].isVisible = !popout[1].isVisible" stop-event="click">Drop Menu #2</a>
<ul ng-show="popout[1].isVisible" stop-event="click">
<li>2222</li>
<li>This is some text.</li>
<li>This is some text.</li>
</ul>


</div>

JavaScript

function p($scope) {
$scope.popout = [ {}, {} ];
$scope.hideEverything = function () {
alert('hiding');
$scope.popout[0].isVisible = false;
$scope.popout[1].isVisible = false;
};
}

angular
.module('app', [])
.directive('stopEvent', function () {
return {
link: function (scope, element, attr) {
element.bind(attr.stopEvent, function (e) {
e.stopPropagation();
alert('ALERT!');
});
}
};
});

关于javascript - AngularJS - 停止传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16343387/

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