Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does angular handles dependency injection for multiple services?

I have created an abstract class that acts as a base service for my other two services. Following is the code snippet for this abstract class:

import { Injectable } from '@angular/core';

export interface Book {
  title: string;
  description: string;
  price: number;
} 
@Injectable({
  providedIn: 'root'
})
export abstract class BaseService {

      constructor() { }

      abstract getBooks(): Book[];

}

Then, Following are two services that implements the abstract class service: First Service:

export class NovelService implements BaseService {

      constructor() { }
      getBooks() {
            const novels: Book[] = [
                {
                  title: 'War and peace',
                  description: "War and Peace broadly focuses on Napoleon's invasion of Russia in 1812",
                  price: 550
                }
              ];

            return novels;
        }

}

Second service:

export class ReferenceBookService implements BaseService {



constructor() { }
getBooks() {
        const referenceBooks: Book[] = [
            {
              title: 'Spring in Action',
              description: "Spring in Action, Fourth Edition is a hands-on guide to the Spring Framework",
              price: 600
            }
          ];

        return referenceBooks;
    }

}

Now, in my component, on click of button I need to decide which service needs to be injected and accordingly execute "getBooks()" function to print the list in html.

I tried using "useClass" attribute in providers array as follows:

import { Component, OnInit } from '@angular/core';
import { Book, BaseService } from '../../services/base.service';
import { NovelService } from '../../services/novels-service';
import { ReferenceBookService } from '../../services/reference-book-service';
let IS_NOVEL_MODE = true;
export function setValue(somevalue) {
  IS_NOVEL_MODE = somevalue;
}
@Component({
  selector: 'app-book',
  template: `<div *ngFor="let book of books">
  <h3>{{ book.title }}</h3>
  <p>{{ book.description }}</p>
</div>
<button (click)="changeMode()">click me!</button>`,
  styleUrls: ['./book.component.css'],
  providers: [{provide: BaseService, useClass: IS_NOVEL_MODE ? NovelService : ReferenceBookService}]
})
export class BookComponent implements OnInit { 
      books: Book[];
      isNovelMode = false;
        constructor(private _baseService: BaseService) { }

          ngOnInit() { 
            this.books = this._baseService.getBooks();
          }
          changeMode() {
            setValue(!this.isNovelMode);
          }

}

I need a way to update providers array or just the "useClass" attribute everytime I click the button.

In a nutshell, having an abstract class BaseService and "NovelService" & "ReferenceBookService" as their implementation, how can I dynamically decide which one of them gets injected in my component?

like image 973
Shruti B Avatar asked Oct 28 '22 01:10

Shruti B


1 Answers

If you use providedIn: 'root' for your services, then Injector for sure has them and you can simply get them:

constructor(private injector: Injector) {}
...
this.myService = this.injector.get(MyService);

And then you don't need to inject baseClass. Also remove @Injectable from it

like image 108
Julius Dzidzevičius Avatar answered Dec 05 '22 12:12

Julius Dzidzevičius