
- Angular Tutorial
- Angular - Home
- Angular - Overview
- Angular - Features
- Angular - Advantages & Disadvantages
- Angular Basics
- Angular - Environment setup
- Angular - First Application
- Angular - MVC Architecture
- Angular Components
- Angular - Components
- Angular - Component Lifecycle
- Angular - View Encapsulation
- Angular - Component Interaction
- Angular - Component Styles
- Angular - Nested Components
- Angular - Content projection
- Angular - Dynamic components
- Angular - Elements
- Angular Templates
- Angular - Templates
- Angular - Template statements
- Angular - Template Variables
- Angular - SVG as Templates
- Angular Binding
- Angular - Data Binding
- Angular - Interpolation
- Angular - Event Binding
- Angular - Property Binding
- Angular - Attribute Binding
- Angular - Class Binding
- Angular - Style Binding
- Angular - Two-way Binding
- Angular Directives
- Angular - Directives
- Angular - Attribute Directives
- Angular - Structural Directives
- Angular - Custom Directives
- Angular Pipes
- Angular - Pipes
- Angular - Built-in Pipes
- Angular - Custom Pipes
- Angular Forms
- Angular - Forms
- Angular - Template Driven Forms
- Angular - Reactive Forms
- Angular - Form Validation
- Angular - Dynamic Forms
- Angular Dependency Injection
- Angular - Dependency Injection
- Angular - Injectable Service
- Angular Routing
- Angular - Routing
- Angular - Dynamic Routes
- Angular - Wildcard Routes
- Angular - Nested Routes
- Angular - Navigation
- Angular - Routing in SPA
- Angular - Custom Route Matches
- Angular - Router Reference
- Angular HTTP Client programming
- Angular - Services
- Angular - HTTP Client
- Angular - Request
- Angular - Response
- Angular - GET
- Angular - POST
- Angular - PUT
- Angular - DELETE
- Angular - JSONP
- Angular - CRUD Operations Using HTTP
- Angular Modules
- Angular - Introduction to Modules
- Angular - Root Module
- Angular - Feature Module
- Angular - Sharing Module
- Angular - Routing Module
- Angular - NgModules
- Angular Animation
- Angular - Animations
- Angular Service Workers & PWA
- Angular - Service Workers & PWA
- Angular Testing
- Angular - Testing Overview
- Angular Design Patterns
- Angular - Design Patterns
- Angular - Lazy Loading
- Angular - Singleton Pattern
- Angular - Observer Pattern
- Angular Libraries
- Angular - Libraries
- Angular - Angular Material
- Angular - PrimeNG
- Angular - RxJS
- Angular Advanced
- Angular - Signals
- Angular - Authentication & Authorization
- Angular - Internationalization
- Angular - Standalone Component
- Angular - Accessibility
- Angular - Web Workers
- Angular - Server Side Rendering
- Angular - Ivy Compiler
- Angular - Building with Bazel
- Angular - Backward Compatibility
- Angular - Reactive Programming
- Angular Tools
- Angular - CLI
- Angular Material UI Elements
- Angular - Paginator
- Angular - Datepicker
- Angular - Select Drop-down
- Angular Miscellaneous
- Angular - Third Party Controls
- Angular - Configuration
- Angular - Displaying Data
- Angular - Decorators & Metadata
- Angular - Basic Example
- Angular - Error Handling
- Angular - Testing & Building a Project
- Angular - Lifecycle Hooks
- Angular - User Input
- Angular - What's New?
- Angular Useful Resources
- Angular - Quick Guide
- Angular - Useful Resources
- Angular - Discussion
Angular - Injectable Service
Injectable Services in Angular
In Angular, an injectable service or simply service is a TypeScript class that can be used to share data or a common feature across different parts of your angular application. A service is defined using @injectable decorator. This decorator tells Angular that we can use instances of the service in dependency injection. In other words, it makes a service injectable.
Dependency injection is a design pattern that is used to inject services and other dependencies into angular components and other classes. In this design pattern, other parts of the application request service objects to perform some operation. The provideIn property of the @injectable decorator metadata specifies where in the angular application the service should be injected.
Creating an Injectable Service
There are two ways to create an injectable service. If you don't want a separate folder for services, use the following command in your IDE's terminal −
ng generate service service-name
The above command will create a service class directly in your Angular application without a separate folder, which can be a bit confusing.
When you want to create services inside a separate folder, use the command given below −
ng generates service Services/service-name
This command will create a Services folder and place the service class inside it. After executing the command, you will see a similar output as given below −
CREATE src/app/Services/service-name.service.spec.ts (389 bytes) CREATE src/app/Services/service-name.service.ts (147 bytes)
Injecting Service at Root Level
To inject a service at the root level, set the value of providedIn property to root. This property is used inside the @Injectable decorator. By default, the service is configured for injecting at the root level, which means a service is shared throughout the whole application.
Suppose we are creating a printName service, this will be the default configuration of the service −
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class PrintNameService { // some code... constructor() { } }
Injecting Service at Component Level
To inject a service in a particular component and its children only, omit the providedIn property in the service class and use the providers array of the specific component where you want to use it.
Service:
import { Injectable } from '@angular/core'; @Injectable({ }) export class PrintNameService { // some code... constructor() { } }
Component:
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', providers: [PrintNameService] }) export class MyComponent { // code }
Injecting Service into Another Service
You can also inject a service into another service. This allows one service to depend on another service. In this type of dependency, Angular follow the same pattern as injecting into a component.
To do this, you simply add the service as a parameter in the constructor of the other service.
import { Injectable } from '@angular/core'; import { MyService } from './my-service.service'; // Make sure to import the service @Injectable({ providedIn: 'root' }) export class AnotherService { constructor(private myService: MyService) {} getAnotherData() { return this.myService.getData(); } }