在AngularJS中打开一个新选项卡。

时间:2022-08-23 22:36:14
<button type="button" class="btn btn-primary" ng-click="openTab()">new tab</button>

openTab = function () {
  $http.post('www.google.com');
}

What I want is post a require and open the response html in a new tab when you click the "openTab" button. There is no method to do this with $http. I think this maybe simple, but I can't find a way.

当你点击“openTab”按钮时,我想要的是post a require,并在新选项卡中打开响应html。没有任何方法可以使用$http来实现这一点。我想这可能很简单,但我找不到办法。

3 个解决方案

#1


157  

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

通过使用这里的$window服务,您可以在控制器中完成这一切。$window是全局浏览器对象窗口的包装器。

To make this work inject $window into you controller as follows

要使这个工作向您的控制器注入$window,如下所示。

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

this works well when redirecting to dynamic routes

这在重定向到动态路由时很有效

#2


14  

I solved this question this way.

我用这种方法解决了这个问题。

<a class="btn btn-primary" target="_blank" ng-href="{{url}}" ng-mousedown="openTab()">newTab</a>

$scope.openTab = function() {
    $scope.url = 'www.google.com';
}

#3


0  

You should use the $location service

您应该使用$location服务。

Angular docs:

角文档:

#1


157  

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

通过使用这里的$window服务,您可以在控制器中完成这一切。$window是全局浏览器对象窗口的包装器。

To make this work inject $window into you controller as follows

要使这个工作向您的控制器注入$window,如下所示。

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

this works well when redirecting to dynamic routes

这在重定向到动态路由时很有效

#2


14  

I solved this question this way.

我用这种方法解决了这个问题。

<a class="btn btn-primary" target="_blank" ng-href="{{url}}" ng-mousedown="openTab()">newTab</a>

$scope.openTab = function() {
    $scope.url = 'www.google.com';
}

#3


0  

You should use the $location service

您应该使用$location服务。

Angular docs:

角文档: