-
Book Overview & Buying
-
Table Of Contents
Angular Cookbook - Second Edition
By :
Can you imagine being able to instantly jump to any place that your eyes can see? That would be awesome! Wouldn’t it? But what if we wanted our app to be able to do that? In this recipe, you’ll create a directive that the user can click to jump to specific sessions in an Angular application.
The app that we are going to work with resides in start/apps/chapter02/ng-scroll-to-directive inside the cloned repository:
npm run serve ng-scroll-to-directive
This should open the app in a new browser tab, and you should see the following:

Figure 2.6: ng-scroll-to-directive app running on http://localhost:4200
scroll-to directive so that we can enhance our application with smooth scrolls to different sections. We’ll do this using the following command in the workspace root folder:
cd start && nx g directive scroll-to --directory apps/chapter02/ng-scroll-to-directive/src/app/directives
If asked, choose the @nx/angular:component schematics and choose the “As provided” action.
@Input() that’ll contain the CSS Query Selector for our target section, which we’ll scroll to upon the element’s click event. Let’s add the input as follows to our scroll-to.directive.ts file:
import { Directive, Input } from '@angular/core';
@Directive({
selector: '[appScrollTo]'
})
export class ScrollToDirective {
@Input() target = '';
}
appScrollTo directive to the links in the app.component.html file along with the respective targets. We’ll replace the href attribute with the target attribute. The code should look like this:
...
<main class="content" role="main">
<div class="page-links">
<h4 class="page-links__heading">
Links
</h4>
<a class="page-links__link" appScrollTotarget=
"#resources">Resources</a>
<a class="page-links__link" appScrollTotarget=
"#nextSteps">Next Steps</a>
<a class="page-links__link" appScrollTotarget=
"#moreContent">More Content</a>
<a class="page-links__link" appScrollTotarget=
"#furtherContent">Further Content</a>
<a class="page-links__link" appScrollTotarget=
"#moreToRead">More To Read</a>
</div>
</main>
...
<a appScrollTo target="#toolbar" class="to-top-button w-12
h-12 text-white flex items-center justify-center">
<span class="material-symbols-outlined text-3xl text-
white"> expand_less </span>
</a>
HostListener() decorator to bind the click event to the element the directive is attached to. We’ll just log the target input when we click the links. Let’s implement this, and then you can try clicking on the links to see the value of the target input on the console:
import { Directive, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appScrollTo]'
})
export class ScrollToDirective {
@Input() target = '';
@HostListener('click')
onClick() {
console.log(this.target);
}
...
}
document.querySelector method, using the target variable’s value to get the element, and then the Element.scrollIntoView web API to scroll to the target element. With this change, you should see the page scrolling to the target element already when you click the corresponding link:
...
export class ScrollToDirective {
@Input() target = '';
@HostListener('click')
onClick() {
const targetElement =
document.querySelector(this.target);
if (!targetElement) {
throw new Error('`target' is required.`);
}
targetElement.scrollIntoView();
}
...
}
scrollIntoViewOptions as an argument to the scrollIntoView method with the {behavior: "smooth"} value to use an animation during the scroll. The code should look like this:
...
export class ScrollToDirective {
@Input() target = '';
@HostListener('click')
onClick() {
const targetElement = document.querySelector
(this.target);
targetElement.scrollIntoView({behavior: 'smooth'});
}
}
The essence of this recipe is the web API that we’re using within an Angular directive, which is Element.scrollIntoView. We first attach our appScrollTo directive to the elements that should trigger scrolling upon clicking them. We also specify which element to scroll to by using the target input for each directive attached. Then, we implement the click handler inside the directive with the scrollIntoView method to scroll to a particular target, and to use a smooth animation while scrolling, we pass the {behavior: 'smooth'} object as an argument to the scrollIntoView method.
scrollIntoView method documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView