in Education by
What is the concept of Dependency Injection?

1 Answer

0 votes
by

Dependency injection is an application design pattern which is implemented by Angular.
It also forms one of the core concepts of Angular.

So what is dependency injection in simple terms?
Let’s break it down, dependencies in angular are nothing but services which have a functionality. Functionality of a service, can be needed by various components and directives in an application. Angular provides a smooth mechanism by which we can inject these dependencies in our components and directives.
So basically, we are just making dependencies which are injectable across all components of an application.
 

image



Let’s understand how DI (Dependency Injection) works:

Consider the following service, which can be generated using:

ng g service test

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

      @Injectable({
        providedIn: 'root'
      })
      export class TestService {
        importantValue:number = 42;

        constructor() { }

        returnImportantValue(){
          return this.importantValue;
        }
      }
    
  

As one can notice, we can create injectable dependencies by adding the @Injectable decorator to a class.

We inject the above dependency inside the following component:

    
      import { TestService } from './../test.service';
      import { Component, OnInit } from '@angular/core';

      @Component({
        selector: 'app-test',
        templateUrl: './test.component.html',
        styleUrls: ['./test.component.css']
      })
      export class TestComponent implements OnInit {
        value:number;
        constructor(private testService:TestService) { }

        ngOnInit() {
          this.value = this.testService.returnImportantValue();
        }
      }
    
  

One can see we have imported our TestService at the top of the page. Then, we have created an instance inside the constructor of the component and implemented the returnImportantValue function of the service.

From the above example, we can observe how angular provides a smooth way to inject dependencies in any component.

Related questions

0 votes
    Top 30+ Angular - Routes and Forms Interview questions and answers...
asked Sep 10, 2021 in Education by JackTerrance
0 votes
    Can one make an angular application to render on the server-side?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    How are observables different from promises in Angular?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    How are Angular expressions different from JavaScript expressions?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    What is Dependency Injection and its types in laravel?...
asked Sep 30, 2021 in Technology by JackTerrance
0 votes
    What is dependency Injection in Spring Boot?...
asked Jul 8, 2021 in Education by JackTerrance
0 votes
    How EJB implements dependency injection?...
asked Nov 7, 2020 in Technology by JackTerrance
+1 vote
    What types of Dependency injection does spring supports? A - Constructor based, Setter based B - Constructor based, ... based D - Constructor based, Setter based, Properties based...
asked Oct 14, 2020 in Technology by JackTerrance
+1 vote
    Which of the following is correct about dependency injection? A - It helps in decoupling application objects from each other. ... . D - It stores object states in file system....
asked Oct 14, 2020 in Technology by JackTerrance
+1 vote
    What is Dependency Injection in Spring? A - It is a design pattern which implements Inversion of Control for software ... - It is used to promote tight coupling in code....
asked Oct 14, 2020 in Technology by JackTerrance
0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I've been following this video( to install bWAPP on Parrot ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Stored procedures are safe from SQL injection attacks (a) True (b) False (c) Depends on the result ... Results topic in portion Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Which of the following option can be considered a target for SQL injection? (1)Misconfigured Databases (2)Excessive Privileges (3)Network Connectivity (4)Stored Procedures...
asked Jun 1, 2021 in Technology by JackTerrance
...