javascript - $anchorScroll 指向链接但无法访问它

标签 javascript html angularjs

我想重定向到同一页面中 DOM 的某些元素。 我使用了以下代码, JS:

 $scope.scrollTo = function (id) {
        $location.hash(id);
        $anchorScroll();
    };

HTML:

<div>
    <div id="chartdiv15" class="chartdiv"></div>
    <div id="chartdata15" class="chartdata"></div>
</div>

和:

     <md-input-container>
        <label>Menu</label>
        <md-select ng-model="menu">
          .....
          <md-option ng-click="scrollTo('chartdiv15')">click</md-option>
        </md-select>
    </md-input-container>

所以这个过程使我能够指向链接( http://localhost:3000/#/graphics#chartdiv15 ) enter image description here

它到达该元素并在一秒钟内重定向到页面顶部,就好像我什么也没做一样...... 有人可以帮我解决这个奇怪的问题吗? 谢谢:)

最佳答案

您是否尝试过不使用 $location.hash(id);但只有 $anchorScroll(id);

根据$anchorscroll的文档:

When called, it scrolls to the element related to the specified hash or (if omitted) to the current value of $location.hash(), according to the rules specified in the HTML5 spec.

在我的项目中,我只使用 $anchorScroll(id),它效果很好,但当然 URL 没有更新。

如果您也希望更新 URL,也许您应该使用:

    $location.hash(id);
    $anchorScroll();   // <- no id here

很抱歉没有直接回答您的问题,但我认为我们无法仅通过提供的数据来帮助您,如果您添加一个重现该问题的 plunker,那么提供建议会更容易。

关于javascript - $anchorScroll 指向链接但无法访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168492/

相关文章:

javascript - Node.js:字符串中有多少位?

javascript - 在 ng-repeat 中显示/隐藏项目

javascript - 似乎无法将每个单词的第一个字母大写

javascript - 在 ng-repeat 模式下编辑和保存数据 - AngularJS

javascript - 在 ng-repeat 中动态应用格式化过滤器

angularjs - $log 中的 Angular ngMocks 错误

javascript - 作者说的 "event handler function wants to unbind itself after it fires"是什么意思

javascript - 如何在视差 slider 中添加视频?

javascript - jquery mobile pageinit 没有看到 div

html - 不会并排放置表格的流畅的两列表格布局