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?
You can do this all within your controller by using the $window service here. $window is a wrapper around the global browser object window.
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.
You can use angular's window service. To open 'url' in a new tab,
$window.open('url');
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.
Make sure the button type is not of type "submit" also or it won't work at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With