DEV Community

Discussion on: Tree Ways to Scroll to an Element using Angular

Collapse
 
mirab20251348 profile image
Mira-B

Use router is not correct solution IMHO. Did you try to click to button "scroll to blue Div" and than scroll up a click the button again? Nothing will happen because URL has been already changed.

Collapse
 
ferfox1981 profile image
Fernando Raposo da Camara Silva

Everyone has a top choice. There's no 100% right or wrong.

Collapse
 
madankumar7 profile image
MadanKumar7

I am trying this now in Angular 12 version, you are gonna need to add the following code in app-routing, for the fragment route to work now. "onSameUrlNavigation: 'reload'" option will resolve the issue.

const routerOptions: ExtraOptions = {
scrollPositionRestoration: 'enabled',
anchorScrolling: 'enabled',
onSameUrlNavigation: 'reload',
};

imports: [RouterModule.forRoot(routes, routerOptions)],

Collapse
 
danny2768 profile image
Daniel Cobos

You just saved me a ton of time.