I have a site using ui-router where I have a table and I have an ng-click on the cells, but have a link inside the cell. I need to disable the ng-click from the cell, when the link is clicked.
<div ng-click="click()" style="background: #d3d3d3">
<a ui-sref="about">go to about page</a>
</div>
When I click this link, I want it to go to the about page, but not call the click function. Here's a plnkr:
This is a little different than my actual site, but this has the same behavior (div instead of table). I've tried adding ng-click="$event.preventDefault()"
but that did not resolve it either.
Any help is appreciated. Thanks!
I have a site using ui-router where I have a table and I have an ng-click on the cells, but have a link inside the cell. I need to disable the ng-click from the cell, when the link is clicked.
<div ng-click="click()" style="background: #d3d3d3">
<a ui-sref="about">go to about page</a>
</div>
When I click this link, I want it to go to the about page, but not call the click function. Here's a plnkr: http://plnkr.co/edit/kE9CZYcYu1OPA9S0K3Jk?p=preview
This is a little different than my actual site, but this has the same behavior (div instead of table). I've tried adding ng-click="$event.preventDefault()"
but that did not resolve it either.
Any help is appreciated. Thanks!
ng-click="$event.stopPropagation()"
on the anchor perhaps so that it does not bubble up to run the click()
on its parent.
– PSL
Commented
May 6, 2015 at 19:16
Prevent propagation of event on the <a>
tag bubbling to the div
<a ui-sref="about" ng-click="$event.stopPropagation()">go to about page</a>
Another way is to check target of click inside the click handler
<div ng-click="click($event)">
JS
$scope.click = function(e){
if( e.target.tagName ==='A'){
return; // skip click event handler
}
// rest of click code
}
You should try $event.stopProgation()
<div ng-click="click();" style="background: #d3d3d3">
<a ui-sref="about" ng-click="$event.stopProgation()">go to about page</a>
</div>
Which will not propagate events to there parents.