当元素多层嵌套的时候,每层都有点击事件,就会发生冒泡,一层一层的触发,但有时候我们只想触发某一层,不想让其他层的事件触发,这就需要阻止冒泡事件了。
在angular项目中,angularJS中的执行方法都会自带一个$event
,这个$event
就是当前事件的对象,通过它来阻止冒泡。
html
<div class="divBox" >
<button ng-click="vpc.test($event)" id="vfwBtn" style="width: 47px;height: 45px;display: inline-block;margin-top:116px;margin-left: 9px;background: none;"></button>
</div>
在对应的controller中,添加阻止冒泡:
1.触发事件,传参数 $event
2.在事件中添加 $event.stopPropagation();
阻止冒泡
self.test = function($event){
$('#showMenus').css('display', 'block');
$event.stopPropagation();
}
另外:
- angular7实例使用
<div class="divBox" >
<button (click)="test('参数');$event.stopPropagation()">
</button>
</div>