user1199272
user1199272

Reputation:

how to make $urlRouterProvider.otherwise route to external url

i have .js file as below

routerApp.config(function ($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise(function ($injector, $location) {
    var $state = $injector.get('$state');
    var path = $location.protocol();
    if (path == 'http') {
       $state.go('external');
    }
    else {
        return '/home';
    }
});

$stateProvider

    // HOME STATES AND NESTED VIEWS ========================================
    .state('home', {
        url: '/home',
        templateUrl: 'routing.html'
    })
    .state('external', {
        onEnter: function ($window) {
         $window.open('http://www.google.com', '_self');
        }
    });

});

above code works fine when path != 'http', it will redirect to routing.html. But when path == 'http', it should call external state and redirect to external url. Here path == 'http' condition is not working. How to call external url here? and in state I am calling external onEnter. Can I do it without OnEnter?

Upvotes: 0

Views: 1872

Answers (2)

Walfrat
Walfrat

Reputation: 5353

This is a bit different but probably something like this will work :)

$rootScope.on("$stateChangeStart", function(event, toState){
    if(toState.name == 'external'){
         event.preventDefault();
         window.open('http://www.google.com', '_self');
    }
});

Upvotes: 0

unflores
unflores

Reputation: 1810

This worked for me:

$urlRouterProvider.otherwise(function ($injector, $location) {
   var protocol = $location.protocol();
   if (protocol == 'http') {
      window.open('http://www.google.com', '_self');
   }
   else {
       return '/home';
   }
});

Upvotes: 1

Related Questions