Angularjs $ state open link in new tab

I am trying to implement the "open link in a new tab" function using the $ state.go function. It would be great if there was something like:

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

Is there a way to do this using AngularJS?

Thank,

Alex

+58
angularjs angular-ui-router angular-ui
May 7 '14 at 11:14
source share
7 answers

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

 var url = $state.href('myroute', {parameter: "parameter"}); window.open(url,'_blank'); 
+121
Mar 16 '15 at 11:04
source share

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

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

It saves the problem of adding code to your controller and gives you a hover url like with any regular link. Win-win!

+50
Jun 15 '14 at 4:26
source share

It may not work on localhost if your application is in a subfolder. I had the same problem.

I tried online and it worked as expected using:

 <a ui-sref="routeHere" target="_blank">Link</a> 
+5
Jan 16 '15 at 9:00
source share

I had a similar problem, try if none of the previous answers work for you.

 var url = '#' + $state.href('preview'); window.open(url,'_blank'); 

So basically when working in localhost without adding '#' it just redirected to

local / preview

instead

local / PROJECT_NAME / # / preview

I'm not talking about data transfer here, just to open $ state in a new tab.

+4
Nov 11 '16 at 13:42 on
source share

Try it!

<a ui-sref="routeHere({parameter: vm.parameter})" target="_blank"></a>

+1
Aug 28 '16 at 18:00
source share
 ui-sref="routeHere" href=""target="_blank" 

this code solved my problem.

use this in the anchor tag.

+1
May 25 '17 at 4:29 a.m.
source share

The best answer I found is the ui.router extension, since the function does not exist. You can find the full information here:

Angular Extension 1.x ui-router $ state.go

However, here is my brief explanation of what needs to be done, add this to app.js or the file with the corner app app:

 angular.module("AppName").config(['$provide', function ($provide) { $provide.decorator('$state', ['$delegate', '$window', function ($delegate, $window) { var extended = { goNewTab: function (stateName, params) { $window.open( $delegate.href(stateName, params, { absolute: true }), '_blank'); } }; angular.extend($delegate, extended); return $delegate; }]); }]); 

In your code

You can:

 $state.goNewTab('routeHere', { parameter1 : "parameter"}); 
0
Jul 27 '18 at 21:02
source share



All Articles