Val Redchenko
Val Redchenko

Reputation: 581

Is it possible to refresh route segment provider in angularjs?

I'm writing an app with AngularJS using routeSegment module. In my app I have the following construct:

.segment('index', {
        templateUrl: 'assets/templates/index.html',
        controller: MainCtrl,
        dependencies: ['index'],
        resolve: ['isUserLoggedIn'],
        resolveFailed: {
            templateUrl:    'assets/templates/login.html',
            controller:     LoginCtrl
        }
})

If user is not logged in, they are redirected to the login page. Problem is, once they's logged in, I call $location.path('/index'); but resolveFailed is still in effect. Is there a way to forcefully refresh routing upon login?

Upvotes: 1

Views: 2487

Answers (2)

Ithinuel
Ithinuel

Reputation: 166

I had the same issue.

The resolveFailed is an alternate segment definition to use in case of resolve failure.

I managed to make it work this way :

var login = {
    controller: 'LoginCtrl',
    templateUrl: 'login.html',
    resolve: { 
        'isUserNotLoggedIn': function($q) {
            var defer = $q.defer();
            if (localStorage.hasOwnProperty("loggedIn")) {
                defer.reject();
            } else {
                defer.resolve();
            }
            return defer.promise;
        }
    }
};

var main = {
    controller: 'MainCtrl',
    templateUrl: 'main.html',
    resolve: {
        'isUserLoggedIn': function($q) {
            var defer = $q.defer();
            if (localStorage.hasOwnProperty("loggedIn")) {
                defer.resolve();
            } else {
                defer.reject();
            }
            return defer.promise;
        }
    },

    /* if user is not loggedin then go to login page */
    resolveFailed: login
};

/* if user is loggedin then go to login page */
login.resolveFailed = main;

.segment('index', main)

I saw on https://github.com/artch/angular-route-segment/issues/23 you worked it around refreshing the entire page.

I also noticed that even if parent segment resolve failed, child segment will still try to load.

Upvotes: 1

sumnulu
sumnulu

Reputation: 3512

Did you try using the reload function?

        $routeSegment.chain[0].reload();

Upvotes: 2

Related Questions