Search code examples
angulardependency-injectionabstract-class

Angular2 dependency injection abstract class


I was going through the internet to find a solution to a random Angular problem and have encountered the following example:

export class AppModule {
  constructor(private router: Router, private viewportScroller: ViewportScroller) {
    this.router.events.pipe(
      filter((e: Event): e is Scroll => e instanceof Scroll),
      pairwise()
    ).subscribe((e: Scroll[]) => {
      const previous = e[0];
      const current = e[1];
      if (current.position) {
        this.viewportScroller.scrollToPosition(current.position);
      } else if (current.anchor) {
        this.viewportScroller.scrollToAnchor(current.anchor);
      } else ...
   ...

The ViewportScroller is an abstract class. I am trying to understand the dependency injection in Angular. How can I make an abstract class injectable?


Solution

  • What will be injected is actual class that extends ViewportScroller. Interfacets can serve as injection tokes as well. What will be injected is implementation of given interface provided by some (if any) provider