Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs $state open link in new tab

I'm trying to implement an "open link in new tab" function using $state.go function. It would be awesome if there was smth like:

$state.go('routeHere', {     parameter1 : "parameter"     }, {     reload : true,     newtab : true // or smth like target : "_blank" }); 

Is there any way to do that using AngularJS?

like image 517
Alex Arvanitidis Avatar asked May 07 '14 11:05

Alex Arvanitidis


People also ask

How to open URL in new tab using AngularJS?

openNewTab = function (url) { $window. open(url, '_blank'); };

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 .

What is the use of $state in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.

What is ui-sref active in AngularJS?

ui-sref-active can live on the same element as ui-sref / ui-state , or it can be on a parent element. If a ui-sref-active is a parent to more than one ui-sref / ui-state , it will apply the CSS class when any of the links are active.


2 Answers

Update: OK, I just solved it using the following code:

var url = $state.href('myroute', {parameter: "parameter"}); window.open(url,'_blank'); 
like image 174
Alex Arvanitidis Avatar answered Sep 21 '22 01:09

Alex Arvanitidis


I just tried this -- apparently, adding target="_blank" works with ui-sref:

<a ui-sref="routeHere" target="_blank">A Link</a> 

Saves the trouble of adding code to your controller, and gives you the URL on hover as with any normal link. Win-win!

like image 34
Patrick Calulo Avatar answered Sep 21 '22 01:09

Patrick Calulo