Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
dependency injection angular

What is Dependency Injection in Angular?

Aman Jain, June 20, 2021November 8, 2023

In a simple term we can say dependency injection in angular are created once at the time of initialisation of service or component then throughout the application we can reuse same object without re-initialisation.

So, this is clear now advantage of dependency injection is to not creating the object in each component and maintain the state of service.

Creating a injectable service:

ng generate service test

it will create a service named test

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

@Injectable({
  providedIn: 'root',
})
export class TestService {
  constructor() { }
}

In the above code @Injectable is a decorator which specifies that Angular can use this class in the DI system.

We can pass multiple metadata in decorator to describe the functionality of component, here we have defined providedIn which means this service can be used in any component.

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


@Injectable({
  providedIn: 'root',
})
export class TestService {
  myTests=[{id:1,name:"test1"}]
  constructor(private logger: Logger) {  }

  getAllTest() {
     
    return this.myTests;
  }
}

We created a function getAllTest and we can use it in our component or other services.

import { Component, OnInit } from '@angular/core';
 
 

@Component({
  selector: 'app-test-service,
  templateUrl: './test-service.component.html',
  styleUrls: ['./test-service.component.css']
})
export class TestServiceComponent implements OnInit {

constructor(testService: TestService){
   testService.getAllTest();
}
}

Hope this make your all doubts clear and you can comment if require any discussion.

Related

Uncategorized

Post navigation

Previous post
Next post

Related Posts

Uncategorized confirm password validation in Laravel

Password and confirm password validation in Laravel

January 26, 2022November 10, 2023

There is many ways to check the password and confirm password validation in laravel. we can match password using the equals to operator but in that case we need to work as core PHP. So In laravel we have Validation library to create the validation rules. Laravel itself provides multiple…

Read More
Uncategorized call api in angular

How to call api in angular 12?

September 10, 2021November 7, 2023

In this tutorial, we are going to call the rest API’s using the angular HttpClient. To fetch data from server we need to call api in angular so we can get the updated data from serve. Angular has vast features and library to call the web or rest API’s. Step…

Read More

What is httpd.conf and httpd-vhost.conf file in apache?

August 28, 2021August 28, 2021

https.conf is main file of apache web server to handle the requests. Apache httpd.conf file generally located at /etc/httpd/conf/httpd, /etc/apache2/ , /etc/apache2/sites-enabled/ in ubuntu. httpd.conf contains the information of server root and port used routing. httpd-vhost contains additional information of virtual host.

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version