AngularJS - 使用ng-click事件在网格中包含的按钮单击

时间:2017-04-01 04:32:28

标签: html angularjs gridview angularjs-ng-click

.html

<div class="col col-50"
    ng-repeat="col in row"
    ng-click="openProductDetails(col.prod_id);">
    <button 
        ng-click="addToCart(col.prod_id);">
        Add to cart
    </button>
</div>

正如您所看到的,我在点击事件中grid<div>openProductDetails()。在网格中,我有一个button,其addToCart()。问题是,当我单击按钮时,openProductDetails()也会被触发。单击按钮时,我找不到阻止父元素的click事件的方法。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用 event.stopPropation() 。此变量是对JS事件对象的引用,可用于调用stopPropagation()

 <div class="col col-50"
        ng-repeat="col in row"
        ng-click="openProductDetails(col.prod_id);">
        <button 
            ng-click="addToCart(col.prod_id); $event.stopPropagation();">
            Add to cart
        </button>
    </div>