AngularJS anchorScroll 不滚动,ui-router

AngularJS anchorScroll not scrolling , ui-router

我正在使用 ui-router 并且我正在尝试在控制器中自动滚动。 然而 anchorScroll 似乎什么都不做,我的猜测是它不喜欢 URL.

中的两个 #

示例:

index.php#/initiative/1/commentscroll/4

变成:

index.php#/initiative/1/commentscroll/4#comment-id-4

但是滚动没有完成(是的,锚点确实存在;)

有什么想法吗?

controllersModule.controller('InitiativeController', ['$http','$timeout','$location','$state','services','$stateParams','$anchorScroll', function($http,$timeout,$location,$state,services,$stateParams,$anchorScroll){
var pk = this;
pk.initiative={};

if($state.current.url.indexOf("/commentscroll/")!=1){
    $timeout(function() {
        $location.hash('comment-id-'+$stateParams.commentId);
        $anchorScroll();
    });
}


services.get($stateParams.initiativeId,'initiative','').then(function(data){
    pk.initiative=data;

});

function fillScrollId(element,index,array){
    if(element.initiative_comment_id===$stateParams.commentId){
        element.scrollToMe="yes";
    }
    if(element.comments.length>0){
        element.comments.forEach(fillScrollId);
    }
}
}]);

我修复了它,这是一个 'timing' 问题..

$location.hash('comment-id-'+$stateParams.commentId);
$timeout(function(){$anchorScroll()}, 800);

$timeout 就可以了!

没有 $anchorScroll 的替代(动画)解决方案

$timeout(function () {
    if ($location.hash()) {
        var anchor = 'a[name="' + $location.hash() + '"]';
        if ($(anchor)) {
            $('html,body').animate({ scrollTop: ($(anchor).offset().top) + 0 }, 'slow');
        }
    }
});