Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to open a file in a new tab?

LIVE DEMO

Given a URI of a file, I'd like to open it in a new tab (not a new window).

It looks like it is not possible to use $window.open(uri, '_blank').

So, I tried the following trick:

var link = angular.element('<a href="uri-here" target="_blank"></a>');
angular.element(document.body).append(link);
link[0].click();
link.remove();

and it works.

But, if I put exactly the same code in a promise callback, it doesn't work anymore (it opens the file in a new window instead).

Any idea what's going on here?

PLAYGROUND HERE

like image 308
Misha Moroshko Avatar asked Jul 11 '14 00:07

Misha Moroshko


1 Answers

From your code/content, you can't force the browser to open a new tab (rather than a new window, or vice-versa). It's up to the browser settings to force it one way or another.

Anything else would be a security risk.

like image 122
David-SkyMesh Avatar answered Sep 21 '22 20:09

David-SkyMesh