在新标签页打开url

时间:2021-07-20 20:13:13

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.

如有任何建议,我们将不胜感激。谢谢你!

4 个解决方案

#1


21  

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

这是一个工作的JSFiddle,在使用之前必须先注入$window:

JS:

JS:

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

HTML:

HTML:

<div ng-app="myApp" ng-controller="dummy"> 
    <a ng-href="" ng-click="redirectToGoogle()">Hello</a>
</div>

#2


13  

Here's another working JSFiddle

这是另一个JSFiddle工作

HTML:

HTML:

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

JS:

JS:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.url ='http://jsfiddle.net/HB7LU/16771/';
}

#3


1  

You can have a directive to do this for you.

你可以有一个指示为你做这件事。

directives.redirect = function ($location, $window, $rootScope) {
    return {

        link: function (scope, element, attrs) {
            element.on("click", function () {
                if (!attrs.newwindow || attrs.newwindow == false) {
                    $location.path(attrs.redirect);
                    scope.$apply();
                }
                else {
                    var baseUrl = $location.$$absUrl.toString().substring(0, decodeURIComponent($location.$$absUrl).toString().indexOf($location.$$path.toString(), $location.$$absUrl.toString().indexOf("#")) + 1) + attrs.redirect;
                    $window.open(baseUrl);
                }
            });
        }
    }
}

And in HTML, you can use the following to open in a new tab:

在HTML中,你可以使用下面的选项卡打开新选项卡:

<a redirect="/admin/overview" newwindow="true">Open in new tab</a>

#4


0  

A simple method to open link in new tab,

在new tab中打开链接的简单方法,

 <a href="{{details.website}}" ng-click="openNewTab(details.website)"> 
    {{details.website}}
 </a>
     $scope.openNewTab = function (url) 
    {
      $window.open(url, '_blank');
     };

Before using it you should inject $window in your controller.

在使用它之前,您应该在控制器中注入$窗口。

#1


21  

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

这是一个工作的JSFiddle,在使用之前必须先注入$window:

JS:

JS:

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

HTML:

HTML:

<div ng-app="myApp" ng-controller="dummy"> 
    <a ng-href="" ng-click="redirectToGoogle()">Hello</a>
</div>

#2


13  

Here's another working JSFiddle

这是另一个JSFiddle工作

HTML:

HTML:

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

JS:

JS:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.url ='http://jsfiddle.net/HB7LU/16771/';
}

#3


1  

You can have a directive to do this for you.

你可以有一个指示为你做这件事。

directives.redirect = function ($location, $window, $rootScope) {
    return {

        link: function (scope, element, attrs) {
            element.on("click", function () {
                if (!attrs.newwindow || attrs.newwindow == false) {
                    $location.path(attrs.redirect);
                    scope.$apply();
                }
                else {
                    var baseUrl = $location.$$absUrl.toString().substring(0, decodeURIComponent($location.$$absUrl).toString().indexOf($location.$$path.toString(), $location.$$absUrl.toString().indexOf("#")) + 1) + attrs.redirect;
                    $window.open(baseUrl);
                }
            });
        }
    }
}

And in HTML, you can use the following to open in a new tab:

在HTML中,你可以使用下面的选项卡打开新选项卡:

<a redirect="/admin/overview" newwindow="true">Open in new tab</a>

#4


0  

A simple method to open link in new tab,

在new tab中打开链接的简单方法,

 <a href="{{details.website}}" ng-click="openNewTab(details.website)"> 
    {{details.website}}
 </a>
     $scope.openNewTab = function (url) 
    {
      $window.open(url, '_blank');
     };

Before using it you should inject $window in your controller.

在使用它之前,您应该在控制器中注入$窗口。