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

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

For example:

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

            })

url assumes

5条回答
  •  借酒劲吻你
    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.

提交回复
热议问题