Javascript AngularJs - 在新标签页中打开网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32198442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs - Open url in new tab
提问by VictorB
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.
任何提示将不胜感激。谢谢你。
回答by michelem
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>
回答by Thomas Dijoux
回答by Gopakumar AP
A simple method to open link in 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.
在使用它之前,你应该注入$window
你的控制器。
回答by Adarsh Konchady
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>
回答by gilm
I used this and it works (angular4+):
<button mat-menu-item>
<a href="/faq/" target="_blank" style="color: inherit; text-decoration: none;">
<mat-icon>help</mat-icon>FAQ
</a>
</button>
我使用了它并且它有效(angular4+):
<button mat-menu-item>
<a href="/faq/" target="_blank" style="color: inherit; text-decoration: none;">
<mat-icon>help</mat-icon>FAQ
</a>
</button>
I had to restore color to default and override browser's colors. This still gives me the ripple effect and everything with zero javascript code.
我不得不将颜色恢复为默认值并覆盖浏览器的颜色。这仍然给了我涟漪效应和零 javascript 代码的一切。