2015-08-25 49 views
11

打開URL我有一個公共頁面和管理頁面的應用程序。我想在登錄後在新標籤頁中打開管理頁面。 我試過這個,登錄成功後,但沒有效果。公共頁面(登錄處)正在重新加載:AngularJs - 在新標籤

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

任何提示將不勝感激。 謝謝。

+1

你能重現錯誤的小提琴並張貼在這裏的鏈接? –

+0

不幸的不是。我無法準確地再現小提琴中的代碼。這太複雜了。 – VictorB

+0

「/ admin/overview」是路由名稱嗎?因爲根據文檔,您不能在URI中使用該方法(http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state)。這可能是問題的一部分 – Sphaso

回答

21

這裏是一個工作JSFiddle,你必須注入$window之前,你可以使用它:

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> 
+1

此作品很好地使用外部URL(如您提供的那個),但在嘗試使用路由器用戶界面(或URL)時不起作用。 – VictorB

11

這裏的另一個工作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/'; 
} 
1

你可以有一個指令來爲你做這個。

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); 
 
       } 
 
      }); 
 
     } 
 
    } 
 
}

而在HTML中,你可以使用下面的一個新選項卡中打開:

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

這裏正在〔實施例:https://jsfiddle.net/aniket_kulkarni/wdzjcgeL/ –

+0

您的解決方案是把#在_blank網址。使用這個$ location。$$ absUrl.split('#')[0] + attrs.redirect; – PSA