How would I have ui-router go to an external link, such as google.com?

前端 未结 5 1354
孤街浪徒
孤街浪徒 2020-11-30 09:52

For example:

 $stateProvider
            .state(\'external\', {
                url: \'http://www.google.com\',

            })

url assumes

相关标签:
5条回答
  • 2020-11-30 10:10

    The original answer is deprecated and disabled by default in UI Router, you may wish to explore implementing it using transition hooks

      .state("mystate", {
        externalUrl: 'https://google.com'
      })
    

    then:

    myApp.run(['$transitions', '$window', ($transitions, $window) => {
      $transitions.onEnter({}, function (transition) {
        const toState = transition.to();
    
        if (toState.externalUrl) {
          $window.open(toState.externalUrl, '_self');
          return false;
        }
        return true;
      });
    }]
    );
    
    0 讨论(0)
  • 2020-11-30 10:15

    As mentioned in angular.js link behaviour - disable deep linking for specific URLs you need just to use

    <a href="newlink" target="_self">link to external</a>
    

    this will disable angularJS routing on a specific desired link.

    0 讨论(0)
  • 2020-11-30 10:16

    Angular-ui-router doesn't support external URL, you need redirect the user using either $location.url() or $window.open()

    I would suggest you to use $window.open('http://www.google.com', '_self') which will open URL on the same page.

    Update

    You can also customize ui-router by adding parameter external, it can be true/false.

      $stateProvider
      .state('external', {
           url: 'http://www.google.com',
           external: true
      })
    

    Then configure $stateChangeStart in your state & handle redirection part there.

    Run Block

    myapp.run(function($rootScope, $window) {
      $rootScope.$on('$stateChangeStart',
        function(event, toState, toParams, fromState, fromParams) {
          if (toState.external) {
            event.preventDefault();
            $window.open(toState.url, '_self');
          }
        });
    })
    

    Sample Plunkr

    Note: Open Plunkr in a new window in order to make it working, because google doesn't get open in iFrame due to some security reason.

    0 讨论(0)
  • 2020-11-30 10:17

    You could use the onEnter callback:

     $stateProvider
        .state('external', {
            onEnter: function($window) {
                $window.open('http://www.google.com', '_self');
            }
        });
    

    Edit

    Building on pankajparkar's answer, as I said I think you should avoid overriding an existing param name. ui-router put a great deal of effort to distinguish between states and url, so using both url and externalUrl could make sense...

    So, I would implement an externalUrl param like so:

    myApp.run(function($rootScope, $window) {
        $rootScope.$on(
            '$stateChangeStart',
            function(event, toState, toParams, fromState, fromParams) {
                if (toState.externalUrl) {
                    event.preventDefault();
                    $window.open(toState.externalUrl, '_self');
                }
            }
        );
    });
    

    And use it like so, with or without internal url:

    $stateProvider.state('external', {
        // option url for sref
        // url: '/to-google',
        externalUrl: 'http://www.google.com'
    });
    
    0 讨论(0)
  • 2020-11-30 10:22

    I transformed the accepted answer into one that assumes the latest version of AngularJS (currently 1.6), ui-router 1.x, Webpack 2 with Babel transpilation and the ng-annotate plugin for Babel.

    .run(($transitions, $window) => {
      'ngInject'
      $transitions.onStart({
        to: (state) => state.external === true && state.url
      }, ($transition) => {
        const to = $transition.to()
        $window.open(to.url, to.target || '_self')
        return false
      })
    })
    

    And here's how the state may be configured:

    .config(($stateProvider) => {
      'ngInject'
      $stateProvider
        .state({
          name: 'there',
          url:'https://google.com',
          external: true,
          target: '_blank'
        })
    })
    

    Usage:

    <a ui-sref="there">To Google</a>
    
    0 讨论(0)
提交回复
热议问题