Angular 2 routing with fragments, async data
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Angular routing supports URL fragments (the #section part of a URL) for in-page navigation and async data loading via route resolvers and guards. Fragments scroll to a specific element on the page, while resolvers pre-fetch data before the component loads. Combining both lets you navigate to a page, wait for async data to load, and then scroll to the specified section. This is common in documentation sites, FAQ pages, and single-page applications with deep linking.
Basic Route Configuration
Using URL Fragments
Route Resolver for Async Data
A resolver fetches data before the component loads, ensuring data is available when the page renders.
Scrolling to Fragment After Async Data Loads
When data loads asynchronously, the target element may not exist yet when the fragment is processed. Use AfterViewInit or a small delay to scroll after rendering.
Functional Resolver (Angular 15+)
Angular 15+ supports functional resolvers without a class.
Common Pitfalls
- Fragment scrolling not working: Angular's
anchorScrolling: 'enabled'must be set inRouterModule.forRoot()options. Without it, fragments in the URL are ignored and no scrolling occurs. This is disabled by default. - Element not found when scrolling: If async data has not rendered yet,
document.getElementById(fragment)returns null. The target element does not exist in the DOM until the data is bound. UseAfterViewCheckedorsetTimeoutto wait for rendering before scrolling. - Resolver blocking navigation: Resolvers delay route activation until the Observable completes. If the API call is slow, the user sees no feedback. Add a loading indicator in a parent component or use a guard with a service instead of a resolver for non-blocking data fetching.
- Fragment not updating on same-route navigation: Navigating from
/article/1#section1to/article/1#section2does not triggerngOnInitbecause the component is reused. Subscribe toroute.fragmentas an Observable to react to fragment changes without component re-creation. - Using
ViewportScrollerincorrectly: Angular providesViewportScroller.scrollToAnchor(fragment)for programmatic scrolling, but it must be called after the view is rendered. Calling it inngOnInitbefore the template renders results in no scrolling.
Summary
- Enable
anchorScrolling: 'enabled'inRouterModule.forRoot()for fragment-based scrolling - Use route resolvers to pre-fetch async data before component activation
- Subscribe to
route.fragmentObservable to react to fragment changes on same-route navigation - Use
AfterViewCheckedto scroll to fragments after async data renders - Angular 15+ supports functional resolvers with
inject()for simpler syntax
Related reading
- Angular Async Validator on multiple form fields
- Angular Custom Async Validator stays in pending state
- Angular make subscribe to wait for response
- Angular Wait for boolean function to finish
- Angular 2 spring boot server side events
- Angular 6 - Could not find module @angular-devkit/build-angular
- Angular with Jasmine is there a conflict between async in beforeEach and async in it?
- AngularJS - wrapping 3rd party asynchronic loaded library as a service
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.