If my url contains route parameters, then hash links reroute in angular

前端 未结 4 1186
小蘑菇
小蘑菇 2020-12-31 12:57

If I have a one level route, then the hash links work as expected with no rerouting. However I have some urls that are country/kh and if I try using hash tags such as countr

4条回答
  •  离开以前
    2020-12-31 13:30

    Alright, I believe the main issue is that Angular handles routing with hashes (sometimes). What you need to do is use the $anchorScroll service. So your JS would look something like:

    function ScrollCtrl($scope, $location, $anchorScroll) {
      $scope.gotoBottom = function (){
        // set the location.hash to the id of
        // the element you wish to scroll to.
        $location.hash('bottom');
    
        // call $anchorScroll()
        $anchorScroll();
      };
    }
    

    And then your HTML could be:

    Go to bottom You're at the bottom!

    http://plnkr.co/edit/De6bBrkHpojgAbEvHszu?p=preview - this is a plunkr (not mine) that demonstrates using $anchorScroll if you need to see it in action

提交回复
热议问题