How to navigate to certain section of the page identified with an id attribute?
Example:
I need to navigate to "structure" paragraph on my page
<div id="info">
<h3>Info</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div id="structure">
<h3>Structure</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
And I have a following navigation structure:
<li>
<ul materialize="collapsible" class="collapsible" data-collapsible="accordion">
<li><a routerLink="policies" class="collapsible-header">Policies</a>
<div class="collapsible-body">
<ul>
<li><a >Info</a></li>
<li><a >Structure</a></li>
<li><a >Something Else</a></li>
</ul>
</div>
</li>
</ul>
</li>
It is my understanding that in Angular1.0 I simply could've navigate to the page section using something like: ui-sref="policies({'#':'structure'})" or href="#structure" or ui-sref="policies" href="#structure"...
How can I do it in Angular2? I looked at the Fragment example in the docs: Query Parameters and Fragments section and I am finding they example to be very confusing and a bit overkill for such a potentially simple task
I have used a combination of the previous answer once a click is performed in the HTML file:
In the component typescript file (component file) it is necessary to define the corresponding navigation including Router and navigation:
With this, the app is able to navigate to the corresponding page but not to the specific section. To go to the defined (desirable) section, it is needed to do the scroll. For that, we initially define a function to do the scroll to specific "id" (Scroll Function here):
Finally, we need to capture the navigation event and perform the scroll (modifying the ngInit function) (Event capturing page Init):
It works for me. Hopes help.
Angular 2: I would prefer to go with scrollIntoView() method scrollIntoView. It would provide smooth scrolling transition effect in the browser.
HTML Code
and in the Component.
You could add
fragment
attribute for your html linkTo navigate programmatically you could this