angularjs $anchorScroll sometimes refresh all page

前端 未结 3 1729
眼角桃花
眼角桃花 2021-01-22 08:38

I have an app with angularjs routing, but on some view i want to scroll to some specific div and i use anchorScroll but sometimes (not all times) it refresh all page even i stop

相关标签:
3条回答
  • 2021-01-22 08:43

    I've two use cases on the same page :

    • When clicking save, if the response is an error, scroll to the errorDiv that displays the error to the user. David Kabii's answer did work for me.
    • However, on the load of the page, in the controller, I want to scroll to a specific area of the page where the user's input is expected. This wouldn't work. I had to use window.setTimeout to workaround this. (there's probably a better way)
          window.setTimeout(function(){
            $location.hash("anchorForm");
            $anchorScroll();
          }, 300);
    
    0 讨论(0)
  • 2021-01-22 08:59

    Try like this

    $scope.redirectTodiv = function(divname,event) {
       var id = $location.hash();
        $location.hash(divname);
        $anchorScroll();
        $location.hash(id);
    
     };
    
    0 讨论(0)
  • 2021-01-22 09:05

    The way to ensure navigation with one click is to combine $location.hash() $anchorScroll and setting routeProvider reloadOnSearch property to false i.e. In your controller code:

    $location.hash("editor");
    $anchorScroll();
    

    In your route provider:

    $routeProvider.when("/masters/voucher", {
        templateUrl: "views/card/voucher.html",
        reloadOnSearch: false
    })
    
    0 讨论(0)
提交回复
热议问题