如何在div中禁用root ng-click按钮?

时间:2016-02-12 12:15:33

标签: javascript html angularjs

我的代码:

<div ng-click="someAction()">
    <button ng-click="otherAction()"></button>
</div>

单击按钮应该只调用otherAction()而不调用someAction()。单击div的其余部分应调用someAction()。

2 个答案:

答案 0 :(得分:4)

您可以使用$event.stopPropagation()来阻止从内部范围到外部范围的传播:

<div ng-click="someAction()">
    <button ng-click="otherAction(); $event.stopPropagation();"></button>
</div>

<div ng-click="someAction()">
    <button ng-click="otherAction($event)"></button>
</div>

修改otherAction代码如下:

$scope.otherAction = function($event) {
    $event.stopPropagation();
    // Existing code
}

答案 1 :(得分:0)

按钮按钮点击

传递事件
<button ng-click="otherAction($event)"></button>

和onAction函数你只需要停止或防止默认

$scope.otherAction = function($event){ $event.stopPropagation; };