我尝试使用 $state.go 函数实现一个“ open link in new tab”函数。如果有这样的东西就太棒了:
$state.go('routeHere', { parameter1 : "parameter" }, { reload : true, newtab : true // or smth like target : "_blank" });
有没有什么方法可以用 AngularJS 做到这一点?
I just tried this -- apparently, adding target="_blank" works with ui-sref:
target="_blank"
ui-sref
<a ui-sref="routeHere" target="_blank">A Link</a>
Saves the trouble of adding code to your controller, and gives you the URL on hover as with any normal link. Win-win!
It may not work on localhost in the event your app is in a subfolder. I had in fact the same issue.
I have tried online and it worked as expected by using:
<a ui-sref="routeHere" target="_blank">Link</a>
Update: OK, I just solved it using the following code:
var url = $state.href('myroute', {parameter: "parameter"}); window.open(url,'_blank');
Try this!
<a ui-sref="routeHere({parameter: vm.parameter})" target="_blank"></a>
I had a similar issue, try this if nothing from previous answers work for you.
var url = '#' + $state.href('preview'); window.open(url,'_blank');
So basically while working in localhost, without appending '#' it was just redirecting to
'#'
localhost/preview
, instead of
localhost/Project_Name/#/preview
I'm not taking here about passing the data, just to open $state in new tab.
ui-sref="routeHere" href=""target="_blank"
this code solved my problem.
use this in an anchor tag.
The best answered I found, was extending the ui.router, since the feature, does not exist build in. You may find the full detail here :
Extending the Angular 1.x ui-router's $state.go
However, here is my short explanation of what needs to be done add this to app.js or angular app init file:
angular.module("AppName").config(['$provide', function ($provide) { $provide.decorator('$state', ['$delegate', '$window', function ($delegate, $window) { var extended = { goNewTab: function (stateName, params) { $window.open( $delegate.href(stateName, params, { absolute: true }), '_blank'); } }; angular.extend($delegate, extended); return $delegate; }]); }]);
In your code
You will be able to do:
$state.goNewTab('routeHere', { parameter1 : "parameter"});