Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow 'Open in a new tab' when using ng-click?

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?

Thanks in advance!

like image 203
jgabrielfaria Avatar asked Apr 14 '14 20:04

jgabrielfaria


People also ask

How to open new tab when clicking link in angular?

To open the link in a new tab, we can use the <a> element by passing a target attribute with a value _blank . Note: In the example above, we have used the href attribute to pass a link instead of routerLink because the routerLink attribute appends the URL to the current URL path.

How to open link in new tab in AngularJS?

A simple method to open link in new tab, <a href="{{details. website}}" ng-click="openNewTab(details. website)"> {{details.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.


2 Answers

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.

EDIT: I would recommend this question if you want a more detailed answer on how to fix this kind of behaviour using JQuery.

like image 131
Erex Avatar answered Oct 05 '22 13:10

Erex


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.

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

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.

like image 26
David Beech Avatar answered Oct 05 '22 13:10

David Beech