Azim
Azim

Reputation: 1091

Angular js not routing to default page after browser page refresh

I have angularjs route with the following definition:

moduleA.config(function($routeProvider){
    $routeProvider.
    when('/A',{templateUrl:'A.jsp'}).
    when('/B',{templateUrl:'B.jsp'}).
    when('/C',{templateUrl:'C.jsp'}).
    otherwise({
        redirectTo: '/',
        templateUrl: 'A.jsp'
    });
});

Now, let say I click on something and it is redirected to #/C/ view. After refreshing the page, it is redirecting to view C and not to the default view. I have to show default page after every page refresh happens. I thought of changing the url to base url while refreshing the page, so that it can be redirected to default page. I am looking for better alternative for this through Angularjs way. Thanks in advance.

Upvotes: 0

Views: 1496

Answers (1)

IsraGab
IsraGab

Reputation: 5175

Try this: In the app.run() block inject 'window' and $location dependency and add:

window.onbeforeunload = function () {
   $location.path('/');
};

Like @maurycy commented, If you want user to go to default page anytime a user he's comming to your application, you don't need the event.

just:

$rootScope.$on('$routeChangeStart', function (event, next, current) {
            if (!current) {
                $location.path('/');
            }
        });

in your app.run() function.

It should work

Upvotes: 3

Related Questions