Html 使用 ng-click 时如何允许“在新标签页中打开”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23069481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to allow 'Open in a new tab' when using ng-click?
提问by jgabrielfaria
I have a table on HTML and each row leads to a different page, with more details about that row. But as I am using angularjs, with ng-click I can't right click this row and select 'open in a new tab'. Is there any way to solve it?
我有一个 HTML 表格,每一行都指向不同的页面,其中包含有关该行的更多详细信息。但是当我使用 angularjs 时,使用 ng-click 我无法右键单击该行并选择“在新选项卡中打开”。有什么办法可以解决吗?
Thanks in advance!
提前致谢!
回答by Erex
If possible you should convert your element to an anchor element.
如果可能,您应该将元素转换为锚元素。
<a ng-href="{{ your dynamic url }}" ng-click="your function">Your link text</a>
The browser will interpret the element as a link, and will therefor give you the correct dropdown. Note that you also have to have the correct href value to open in a new tab.
浏览器会将元素解释为链接,并因此为您提供正确的下拉列表。请注意,您还必须拥有正确的 href 值才能在新选项卡中打开。
EDIT:I would recommend this questionif you want a more detailed answer on how to fix this kind of behaviour using JQuery.
编辑:如果您想获得有关如何使用 JQuery 修复此类行为的更详细答案, 我会推荐这个问题。
回答by David Beech
Inside your ng-click function you can call window.open(url, '_blank')
however as a word of warning, this will depend on the browser and current settings.
在你的 ng-click 函数中,你可以调用window.open(url, '_blank')
但是作为警告的话,这将取决于浏览器和当前设置。
In some cases this will open in a pop-out window and in other cases it will open in a new tab. There is no way to force either behavior as the javascript is browser agnostic, it has simply requested a new window and it is up to the browser the decide how to implement it. see here for a discussion on forcing a tab or window
在某些情况下,这将在弹出窗口中打开,而在其他情况下,它将在新选项卡中打开。没有办法强制任何一种行为,因为 javascript 是浏览器不可知的,它只是请求一个新窗口,由浏览器决定如何实现它。有关强制选项卡或窗口的讨论,请参见此处
However the only way to get that right-click option or the ctrl+click to open in a new tab is if the browser sees a <a>
tag. Otherwise it doesn't treat it as a link.
但是,获得该右键单击选项或 ctrl+单击以在新选项卡中打开的唯一方法是浏览器是否看到<a>
标签。否则它不会将其视为链接。
回答by Rubi saini
If you want to generate your href
dynamically based on some condition then you can set your href
on ng-mousedown
event and after that you can perform any event like open link in new tab
, open link in new window
and click
.
如果您想href
根据某些条件动态生成您的事件,那么您可以设置您的href
onng-mousedown
事件,然后您可以执行任何事件,例如open link in new tab
,open link in new window
和click
。
HTML:
HTML:
<a href="javascript:void(0)" ng-mousedown="openDetailView($event, userRole)">{{label}}</a>
JS :
JS:
$scope.openDetailView = function (event, userRole) {
if(userRole == 'admin') {
jQuery(event.target).attr('href', 'admin/view');
} else {
jQuery(event.target).attr('href', 'user/view');
}
};
回答by Fizer Khan
You have to use anchor element inside the table row.
您必须在表格行内使用锚元素。
HTML
HTML
<tr>
<a ng-href="dynamic url" ng-click="openItem($event)">Open the item</a>
</tr>
Controller
控制器
$scope.openItem = function (event) {
event.preventDefault();
// code to open a item
}