0

Im using angular 1 for my app + ui router. I want the app to return to the initial state if page reloads. Currently, the app stays on the same page it was before, even after the browser refreshes. How can I make the app to return to login state after page reload? This is my app.js with the config data:

app.config(function($stateProvider, $urlRouterProvider) {
    var loginState = {
        name: 'login',
        url: '/login',
        templateUrl: 'templates/login/login.html',
        controller: "loginController"
    }

    var homePageState = {
        name: 'homePage',
        url: '/homePage',
        templateUrl: 'templates/chatRoom/homePage.html',
        controller: "homePageController"
    }

    $stateProvider.state(loginState);
    $stateProvider.state(homePageState);
    $urlRouterProvider.otherwise('login');
}).run([
    "$state",
    function($state) {
        $state.go('login');
    }
]);
Alex
  • 1,982
  • 4
  • 37
  • 70
  • 1
    possible duplicate https://stackoverflow.com/questions/39480045/how-to-redirect-to-home-view-after-page-refresh-angularjs https://stackoverflow.com/questions/28072092/how-to-redirect-on-different-view-on-refresh-in-angularjs – mygeea Feb 02 '18 at 00:46

2 Answers2

0
app.config(function($stateProvider, $urlRouterProvider) {
    //...
}).run([
    "$state",
    "$rootScope",
    function($state, $rootScope) {

        $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
            if (toState.name == fromState.name) {
                $state.go('login');
            }
        });
    }
]);

when refresh, the name of router is same. hope this helps you.

Aison
  • 157
  • 2
  • 10
0

There is a simple javascript event which you can use.

<script>
        window.onbeforeunload = function () {
            window.location.href = 'http://localhost:61632/index.html#/login';
//Here set the path where you wanted your application to go after reload.
        };
    </script>

Just add this script tag to your index html file and you are done.

Rakesh Burbure
  • 1,045
  • 12
  • 27