Angularjs $state open link in new tab

前端 未结 7 2055
遇见更好的自我
遇见更好的自我 2020-12-01 00:36

I\'m trying to implement an \"open link in new tab\" function using $state.go function. It would be awesome if there was smth like:

$state.go(\'routeHere\',          


        
相关标签:
7条回答
  • 2020-12-01 00:59

    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>
    
    0 讨论(0)
  • 2020-12-01 01:00

    Update: OK, I just solved it using the following code:

    var url = $state.href('myroute', {parameter: "parameter"});
    window.open(url,'_blank');
    
    0 讨论(0)
  • 2020-12-01 01:04

    Try this!

    <a ui-sref="routeHere({parameter: vm.parameter})" target="_blank"></a>

    0 讨论(0)
  • 2020-12-01 01:19

    I just tried this -- apparently, adding target="_blank" works with 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!

    0 讨论(0)
  • 2020-12-01 01:22

    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.

    0 讨论(0)
  • 2020-12-01 01:23

    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"});
    
    0 讨论(0)
提交回复
热议问题