Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open new tabs with AngularJS

Tags:

angularjs

I need to know, how can I open a link in a new tab/window with Angular. I have a regular website and when user click in a button "already registered", I need to open a new tab with a login page.

Should I use $location to map the path? Best practices?

like image 493
vitorvigano Avatar asked Apr 23 '13 14:04

vitorvigano


People also ask

How to open a new tab in AngularJS?

You can do this all within your controller by using the $window service here. $window is a wrapper around the global browser object window.

What is $window in AngularJS?

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.


3 Answers

You can use angular's window service. To open 'url' in a new tab,

$window.open('url');
like image 104
0xc0de Avatar answered Oct 05 '22 00:10

0xc0de


If you want to open a new tab I would suggest having your $routeProvider have a '/login' route and simply have your button link to '/login', however, keep in mind you'll be reloading the entire app if you wish to keep the login page within the application when opening a new tab.

You could combine it with something like this to open a small login window separate of the app as well, and listen for the window to close and handle the response.

like image 35
Ryan Q Avatar answered Oct 04 '22 23:10

Ryan Q


Make sure the button type is not of type "submit" also or it won't work at all.

like image 1
arush436 Avatar answered Oct 05 '22 01:10

arush436