Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs - Open url in new tab

I have an application with a public page and an admin page. I would like open the admin page in a new tab after login. I've tried this, after the login was successful, but to no effect. The public page (where the login is) is just being reloaded:

var url = $state.href('/admin/overview');
$window.open(url, '_blank');

Any tips would be appreciated. Thank you.

like image 589
VictorB Avatar asked Aug 25 '15 07:08

VictorB


People also ask

How to open link in new tab using AngularJS?

openNewTab = function (url) { $window. open(url, '_blank'); };

What is $window in AngularJS?

The $window service refers to the browser window object. It is globally available in JavaScript, so it causes testability problems. In AngularJS, it is not globally available. It includes various methods like alert box, prompt box, confirms box, etc.


2 Answers

Here is a working JSFiddle, you have to inject $window before you can use it:

JS:

angular.module('myApp', [])
    .controller('dummy', ['$scope', '$window', function ($scope, $window) {
    $scope.redirectToGoogle = function () {
        $window.open('https://www.google.com', '_blank');
    };
}]);

HTML:

<div ng-app="myApp" ng-controller="dummy"> 
    <a ng-href="" ng-click="redirectToGoogle()">Hello</a>
</div>
like image 68
michelem Avatar answered Sep 23 '22 16:09

michelem


Here's another working JSFiddle

HTML:

<div ng-controller="MyCtrl">
<a ng-href="{{url}}" target="_blank">Visit jsfiddle</a>
</div>

JS:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.url ='http://jsfiddle.net/HB7LU/16771/';
}
like image 38
Thomas Dijoux Avatar answered Sep 22 '22 16:09

Thomas Dijoux