0

When navigating a site that's using angularjs when changing routes it adds a hashbang at the end and can resume the state on refresh (which is very cool).

However in my particular project I would like it on refresh to load the main route (like it would if there would be no hashbang).

Is this possible?

1
  • my understanding is that you want to make the browser on refresh to navigate to a different URL. and you want to accomplish this from javascript. I do not see how this is possible from javascript. In addition, this won't be a refresh anymore, but something else, which may upset the user. Commented May 10, 2013 at 5:25

1 Answer 1

1

You can do this by using either a service or multiple controllers. Easiest way IMO is to set up a simple controller and service that is included on every page.

Something like this:

<div style='display:none;' ng-controller='RefreshController'></div>

Service:

.factory('redirect',function($rootScope){
    var rScope={};
    rScope.redirect=true;
    rScope.set=function(){
        rScope.redirect=false;
    };
    rScope.get=function(){
        return rScope.redirect;
    };
    return rScope;
});

Controller:

function RefreshController($location,redirect){
    if($location.path()=='/'){
        redirect.set()
    }
    if(redirect.get()){
        $location.path('/');
    }
}

In a nutshell, if you are on the main page, it will set a service variable to say that you are on the home page. As long as no refresh is done, the service variable state will be static. If a refresh is done and you are not currently on the main page, the service variable will be different, and the user will be redirected to the main page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.