4

I have a peculiar problem .I am using anchorScroll service to scroll the page to top to show any messages,like for error or save success messages. I am implementing the function in save button action.

What happens here that when I hit the button first time, it refreshes the page and then when I use it again ,I am getting the required result.

I would like to somehow stop it from refreshing the page first time around.

This button is outside the form directive,although my form is inside form tag. Included $anchorScroll service

<a id="top"></a>   at the top of my HTML page.

if(data.validateError==true )
                {
                    $scope.saveSuccessfull=false;
                    $scope.wrongInputError=true;
                    $scope.negErrorMessage=false;
                    $scope.loaderDiv=false;
                    $location.hash('top');
                     // call $anchorScroll()
                   $anchorScroll();

3 Answers 3

14

From this answer : https://stackoverflow.com/a/15935517/971 which proved useful to me having a similar issue.

var old = $location.hash();
$location.hash('top');
$anchorScroll();
//reset to old to keep any additional routing logic from kicking in
$location.hash(old);

This will prevent the reload.

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

2 Comments

If only I could give you three up votes for this great solution. Was wracking my brain for awhile.
Why this is not in the official docs example, really annoys me. Was about to custom roll some JS....
3

The reloadOnSearch: false solution suggested by @Raulucco worked fine for me. Following an example

$routeProvider
.when('/search', {
           templateUrl : 'views/search.html', reloadOnSearch: false
 })

Comments

1

I found this answer after having the same problem. I resolve it by creating a new service that makes the scroll with jQuery. but I found on the documentation that on the routeProvider is possible to set the attribute reloadOnSearch to false, so that now you can change the hash and the view won't reload. I found it a cleaner way.

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.