gpt4 book ai didi

javascript - Angular 的 $anchorScroll 与 ng-show 配合不佳

转载 作者:行者123 更新时间:2023-11-28 06:49:57 24 4
gpt4 key购买 nike

我有一个应用程序,一旦单击按钮,它就会显示 JSON 文件中的数据 (ng-show)。

我遇到的问题是anchorScroll,只有在单击按钮两次后,它才会滚动到 anchor 。我需要它在第一次点击时工作。

这里是魔法应该发生的地方,但没有发生

var onSearchComplete = function(results){
$scope.showResults = true; //ng-show listens for this to be true
$scope.results = results;

$timeout(function() {
$location.hash('resultsAnchor');
$anchorScroll();
});
};

这是我所知道的:

  • 第一次点击:最初我的 URL 以 url/#/ 开头,然后当单击按钮时,已附加 #resultsAnchor,因此我的 URL 现在为 url/#/#resultsAnchor。然后,带有我的数据的 ng-show div 将出现在实例中消失。由于没有数据,因此不会出现anchorScroll。
  • 现在我的 URL 设置为 url/#/#resultsAnchor第二次点击ng-show div 出现,页面滚动到数据。

是什么可以一键阻止这种情况发生?

注意:我添加了 $timeout 函数,否则需要点击三下。单击 1 - anchor 附加到 URL,单击 2 - 出现数据,单击 3 - 页面滚动。

最佳答案

我刚刚遇到了类似的问题,通过将位置的前缀添加到哈希中来纠正它。

location.hash = '/details#memberTop';
$anchorScroll();

最终导航到:

url/#/details#memberTop

关于javascript - Angular 的 $anchorScroll 与 ng-show 配合不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com