[英]Angular - how to link from one page to another page and scroll to an anchor in ionic without using id
I am developing an ionic app. 我正在开发一个离子应用程序。 Which has a listing of articles on my home page, and each article has a comment icon which should serve as a link to comment section in the article page.
其中包含我的主页上的文章列表,每篇文章都有一个评论图标,该图标应作为文章页面中评论部分的链接。
How can I link from that front page to go to an anchor on the comment section in the article page? 如何从该首页链接到文章页面评论部分的锚点?
Updated 更新
Since Angular uses "URLs" after the # , it won't work to link to an anchor by id, that was suggested in the answer, so I am looking for another way to do it. 由于Angular在#之后使用“URL”,因此无法通过id链接到锚点,这是在答案中建议的,所以我正在寻找另一种方法来实现它。 I have tried several solutions but none of the following didn't work.
我尝试了几种解决方案,但以下都没有效果。
Here, sometimes I get a correct value for top offset, but when I change to another article page, the value stays the same. 在这里,有时我得到一个正确的顶部偏移值,但是当我改为另一个文章页面时,该值保持不变。
This is the code I have done: 这是我做的代码:
Article list View: 文章列表查看:
<a ng-click="commentLink({{article.id}})">
<img class="social-images" src="icons/comment.svg"/> {{ article.comments.length }}
</a>
Article View: 文章查看:
<a name="comments-section"></a>
Controller: 控制器:
$scope.commentLink = function(articleId) {
return $state.go('main.article', {id: articleId}).then(function(state) {
var position = $ionicPosition.offset(angular.element(document.getElementsByName('comments-section')));
console.log(position.top);
$ionicScrollDelegate.scrollTo(position.left, position.top);
})
};
I have also tried this, based on an this article , but still no luck: 我也试过这个,基于这篇文章 ,但仍然没有运气:
Controller: 控制器:
$location.hash('comments-section');
var handle = $ionicScrollDelegate.$getByHandle('articleDelegate');
handle.anchorScroll(true);
View: 视图:
<ion-content delegate-handle="articleDelegate">
<a id="comments-section"></a>
If your comment section has an id (eg. id="new-comment"), then from the front page, you can try: 如果你的评论部分有一个id(例如id =“new-comment”),那么从头版开始,你可以尝试:
<a ng-click="goToComment()">...</a>
And in controller: 在控制器中:
$scope.goToComment = function() {
return $state.go('whatever the comment state is', {param:param}).then(function(state) {
$location.hash('new-comment');
$ionicScrollDelegate.anchorScroll(true)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.