简体   繁体   English

在 AngularJS 中单击按钮打开一个新选项卡

[英]Open a new tab on button click in AngularJS

<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.我想要的是当您单击“openTab”按钮时发布一个要求并在新选项卡中打开响应 html。 There is no method to do this with $http .没有办法用$http做到这一点。 I think this maybe simple, but I can't find a way.我认为这可能很简单,但我找不到方法。

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 是全局浏览器对象窗口的包装器。

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这在重定向到动态路由时效果很好

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

Proper HTML way: just surround your button with anchor element and add attribute target="_blank" .正确的 HTML 方式:只需用锚元素包围按钮并添加属性target="_blank" It is as simple as that:就这么简单:

<a ng-href="{{yourDynamicURL}}" target="_blank">
    <h1>Open me in new Tab</h1>
</a>

where you can set in the controller:您可以在控制器中设置的位置:

$scope.yourDynamicURL = 'https://stackoverflow.com';

You should use the $location service你应该使用$location服务

Angular docs:角度文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome扩展程序:打开新标签页,然后点击按钮 - Chrome extension: Open new tab and click on button 单击按钮,在新选项卡上打开链接 - Open link on a new tab on click of button 鼠标右键单击并在新选项卡中打开在angularjs中不起作用 - Mouse right click and open in new tab does not work in angularjs 使用ctrl + click在新选项卡中打开AngularJS $状态链接 - AngularJS $state open link in new tab using ctrl + click 使用Angularjs在按钮单击的另一个选项卡上打开html - Open html on another tab on button click using Angularjs 如何使用 Playwright 打开新选项卡(例如单击按钮在新选项卡中打开新部分) - How to open the new tab using Playwright (ex. click the button to open the new section in a new tab) 通过单击Java脚本中的按钮来打开新的浏览器选项卡 - Open new browser tab from button click in java script 单击按钮在浏览器的新选项卡中打开 sapui5 详细信息页面 - open sapui5 detail page in a new tab of the browser on click of a button 单击按钮:打开一个新的浏览器选项卡并调用一个函数 - Button Click: open a new browser tab and call a function 如何在vueJs中不点击按钮的情况下在新标签页中打开页面 - How to open page in new Tab without button click in vueJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM