admin管理员组文章数量:1026989
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!
Share Improve this question asked May 6, 2015 at 19:08 TimothyTimothy 1,1603 gold badges12 silver badges33 bronze badges 4-
ng-click="$event.stopPropagation()"
on the anchor perhaps so that it does not bubble up to run theclick()
on its parent. – PSL Commented May 6, 2015 at 19:16 - @PSL that worked. You're awesome! If you put this as an answer, I'll accept it. – Timothy Commented May 6, 2015 at 19:21
- 1 Timothy - @:charlietfl has updated his answer since, which you may accept.. – PSL Commented May 6, 2015 at 19:22
- @Timothy: Rather than adding an unnecessary ng-click event on your link, try to use $event.currentTarget and execute you event handler code. This don't require you to add ng-click on anchor tag. Because you have anchor tag in table column you will end up with lot of event handler for anchor tags. So I think performance wise this would be better with only required event handler on your table cell. – Jagadish Dharanikota Commented May 6, 2015 at 19:28
2 Answers
Reset to default 6Prevent 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.
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!
Share Improve this question asked May 6, 2015 at 19:08 TimothyTimothy 1,1603 gold badges12 silver badges33 bronze badges 4-
ng-click="$event.stopPropagation()"
on the anchor perhaps so that it does not bubble up to run theclick()
on its parent. – PSL Commented May 6, 2015 at 19:16 - @PSL that worked. You're awesome! If you put this as an answer, I'll accept it. – Timothy Commented May 6, 2015 at 19:21
- 1 Timothy - @:charlietfl has updated his answer since, which you may accept.. – PSL Commented May 6, 2015 at 19:22
- @Timothy: Rather than adding an unnecessary ng-click event on your link, try to use $event.currentTarget and execute you event handler code. This don't require you to add ng-click on anchor tag. Because you have anchor tag in table column you will end up with lot of event handler for anchor tags. So I think performance wise this would be better with only required event handler on your table cell. – Jagadish Dharanikota Commented May 6, 2015 at 19:28
2 Answers
Reset to default 6Prevent 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.
本文标签: javascriptDisable ngclick from outside element on link insideStack Overflow
版权声明:本文标题:javascript - Disable ng-click from outside element on link inside - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745666196a2162191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论