[Angular应用程序]:导航到另一页并滚动到特定部分

huangapple go评论73阅读模式
英文:

[Angular Application]: Navigate to another Page and Scroll to a particular section

问题

我们可以通过添加带有ID的锚元素,并将该ID粘贴为链接中的片段,从而使页面滚动到该部分。

但是,这里的挑战是,如果我们在同一页中添加片段和锚点,它会滚动到该部分。
但我正在导航到不同的页面,并且在导航时添加了片段,但它不会滚动到该部分。

原因是由于子组件的懒加载。在导航时,这些组件尚不可用,这就是不滚动的原因。

一种解决方法是在ngAfterViewInit中添加一些JavaScript功能,比如ScrollIntoView。但是,如果可能的话,希望找到其他方法。

将所有这些词语放在一行中点击按钮后,在Angular应用程序中导航到另一页并滚动到特定部分

英文:

We can add a anchor element with id and by pasting the id as fragment in the link it will scroll to that section.

But the challenge here is, If we add fragment and anchor present in same page it is going to scroll.
But I am navigating to different page and fragment is being added while navigating, but is not scrolling to that section.

Reason is due lazy loading of child components. When Navigating, the components are not available yet, and that's reason not scrolling.

A workaround is to add some javascript functionality in ngAfterViewInit like ScrollIntoView. But Looking for any other approaches if possible.

Putting all these words in line On Click of Button, Navigate to another page and scroll to particular section in Angular Application

答案1

得分: 1

使用Router.Navigate非常简单。您可以使用要导航到的元素的IDfragment

英文:

Its pretty straightforward with Router.Navigate. you can use the fragment with the ID of the element you want to navigate to.

huangapple
  • 本文由 发表于 2023年3月9日 13:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定