angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

Forcing router refresh

Open vredchenko opened this issue 12 years ago • 7 comments

Already on SO: http://stackoverflow.com/questions/19812911/is-it-possible-to-refresh-route-segment-provider-in-angularjs

Basically, I'm using:

resolve: ['isUserLoggedIn'], resolveFailed: { templateUrl: 'assets/templates/login.html', controller: LoginCtrl }

to redirect to login page if user is not yet logged in. The login controller redirects back to /#/index if user logged in successfully, but it seems that templateUrl and controller have already been decided and they don't get refreshed, isUserLoggedIn does not get called for the second time. Is this intended behaviour? Is there a way to forcefully refresh routing after initial setup?

vredchenko avatar Nov 06 '13 13:11 vredchenko

Did you find a solution for this? I have run into the same problem.

pbaylas avatar Nov 18 '13 16:11 pbaylas

This seems to do the trick:

.segment('index', {
            templateUrl: 'assets/templates/index.html',
            controller: IndexCtrl,
            dependencies: ['index'],
            resolve: {
                data: function(Article, $q, $location) {
                    var deferred = $q.defer();
                    Article.getAll().get({},
                        function(successData) {
                            deferred.resolve(successData);
                        }, function(errorData) {
                            deferred.reject();
                        });
                    return deferred.promise;
                }
            },
            untilResolved: {
                templateUrl: 'assets/templates/loading.html'
            },
            resolveFailed: {
                // ToDo: Error template and controller should be here.
                templateUrl: 'assets/templates/login.html',
                controller: LoginCtrl
            }})

vredchenko avatar Nov 18 '13 17:11 vredchenko

What does your LoginCtrl do on a successful login to reload the index segment?

pbaylas avatar Nov 19 '13 10:11 pbaylas

$location.path('/index');

vredchenko avatar Nov 19 '13 10:11 vredchenko

We've still got a bug with this approach - we'll post a solution here (and on SO) once we've fixed it.

vredchenko avatar Nov 19 '13 10:11 vredchenko

FYI we ended up with a hack, forcefully refreshing the entire page in order to refresh the router. Had a deadline and no time to play around and get to the bottom of things :(

vredchenko avatar Nov 20 '13 18:11 vredchenko

Did you have a look to watcher property?

.segment('index', {
    ...
    watcher: function(injectables) {
        ...
        return result; // when result is changed, the segments gets reloaded
    }
})

artch avatar May 13 '14 08:05 artch