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

angularjs, javascript

Solution

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.

Problem

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

Original source

Related problems