这是一个例子.假设我希望像许多网站一样覆盖图像.因此,当您单击缩略图时,整个窗口上会出现黑色叠加层,并且图像的较大版本将居中.点击黑色叠加层即可解散它; 单击图像将调用显示下一个图像的功能.
html:
<div ng-controller="OverlayCtrl" class="overlay" ng-click="hideOverlay()">
<img src="http://some_src" ng-click="nextImage()"/>
</div>
Run Code Online (Sandbox Code Playgroud)
javascript:
function OverlayCtrl($scope) {
$scope.hideOverlay = function() {
// Some code to hdie the overlay
}
$scope.nextImage = function() {
// Some code to find and display the next image
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,这种设置,如果你点击图片,都nextImage()与hideOverlay()被调用.但我想要的只是nextImage()被召唤.
我知道你可以在这个nextImage()函数中捕获和取消事件,如下所示:
if (window.event) {
window.event.stopPropagation();
}
Run Code Online (Sandbox Code Playgroud)
...但我想知道是否有更好的AngularJS方法,不需要我使用此片段为叠加层内的元素添加所有函数的前缀.
有以下代码:
<div class="first" ng-click="funcFirst()">
... Some other content
<div class="second" ng-click="funcSecond()">
</div>
... Some other content
</div>
Run Code Online (Sandbox Code Playgroud)
我想做以下事情:如果我点击"第一"div中除"second"之外的任何内容 - 执行funcFirst函数; 如果我点击"秒"div - 执行funcSecond函数.现在,如果我点击"秒",我执行funcFirst.我该怎么做?我无法改变HTML结构.提前致谢.