0

I have a navigation menu with links to different sections. This navigation bar is a list which is only displayed by clicking on the button 'menu'. I would like that on load of the new section this list is not displayed.

Therefore I thought I could use $location.path() to detect any change on the url and if so remove the class.

The idea is:

if($location.path() CHANGES){
    document.querySelector('[data-navigation]').classList.remove('open-status');
}

How do you make a conditional statement using $location to trigger an action when the url changes.

P.D. The url at this moment works with an hashtag. So it would be: www.whatever.com/#about

2 Answers 2

1

i guess you probably meant using $routeChangeSuccess, something like,

$scope.$on('$routeChangeSuccess', function () {
     //location changed
     //do something here            
});
Sign up to request clarification or add additional context in comments.

2 Comments

there is also a $routeChangeStart, $locationChangeSuccess, $locationChangeStart that could be used depending on situation
@maurycy What are the main differences? I already had injected $location so I will try it out with your answer.
1

At the end I used a combination of both answers, this is the outcome that worked for me:

scope.$on('$locationChangeSuccess', function () {
    DO SOMETHING
});

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.