[英]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.