2

我正在使用 AngularJS 的 locationhash() + $anchorScroll 将选定的页面元素移动到窗口顶部,一旦其内容通过 Ajax 加载。

JS:在控制器中:

     $scope.scrollTo = function (location) {
            //Scroll to category head
            $scope.categoryHead = "grouptitle-" + location;
            $location.hash($scope.categoryHead);
            $anchorScroll($scope.categoryHead);
     };

在指令中:

 .directive('onFinishRender', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                var scroll;
                if (scope.$last === true) {
                    $timeout(function () {
                        //Scroll category to top of page after list has completed render
                        scroll = scope.scrollTo(scope.category);
                    });
                }
            }
        };

这给了我一个或类似的显示 URL mysite.com/##grouptitle-2,看起来有点神秘。有什么方法可以配置这个锚,使其要么只显示一个哈希,要么根本不修改地址栏 URL?

4

3 回答 3

0

在您$anchorScroll删除参数时。anchorScroll 会自动从$location.hash(<hash>).

所以你的函数应该是这样的:

$scope.categoryHead = "grouptitle-" + location;
$location.hash($scope.categoryHead);
$anchorScroll();

如果 DOM 是动态更新的,则包装$location.hashand $anchorScrollinside$timeout函数。

于 2015-07-23T18:40:39.620 回答
0

执行以下步骤:在此之后 $location.hash($scope.categoryHead); 添加

$anchorScroll();
$location.hash('');
$location.replace();
    

$location.replace() 函数将从 url 中删除 '#'。

于 2016-12-21T12:39:02.407 回答
0

执行以下步骤:在此之后 $location.hash($scope.categoryHead); 添加

$anchorScroll();
$location.hash('');
$location.replace();

$location.replace() 函数将从 url 中删除 '#'。

于 2016-12-21T13:35:23.367 回答