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

How to create a service in angular?

Aman Jain, September 11, 2021October 3, 2022

Services are same as class with a specific purpose of it. Services can contain method, property and any feature of an application which we can reuse multiple time in our web application. Services also contain a decorator which tell angular how to consume it in our application.

Let’s create a service.

Install a fresh or existing angular project and follow the steps:

Step 1 : Create a service

ng generate service logger

Above code will create a service in src\app\logger.service.ts or we can create manually

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

@Injectable({
  providedIn: 'root'
})
export class LoggerService {

  constructor() { }
}

Step 2 : Inject service in component

You can inject the service in new component or existing component and can use all the methods and properties of it.

import { Component } from '@angular/core';
import { LoggerService } from './logger.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  title = 'Service Example';
  constructor(logger:LoggerService){

    logger.log("This is test")

  }  
}

Here, we import the LoggerService and injected it in constructor, then we can use the methods of LoggerService in this component.

Step 3 : Run and check in console

Run using angular command and check the simple use of services

ng serve

Advantages of service or dependency injection

  1. Services are globally accessible by angular app
  2. Using Dependency injection, Angular create the object once and share it with all component so it saves the memory and sustain the state.
  3. Using Services we can communicate between n level component using EventEmitter.

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

Javascript reusable components in angular

How to make reusable components in angular?

September 11, 2021October 4, 2022

Another rich feature of angular is reusability of component, Angular components are those components which we can reuse multiple times in a module or application. Angular reusable component can be dynamic and much flexible to send updates on any event . Before start, I assume you well aware of installation…

Read More
Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

Read More
Javascript Min Length and Max Length Validation in Angular 12 13

Min Length and Max Length Validation in Angular 12 / 13 ?

March 21, 2022March 21, 2022

Angular provides built-in library for validation, in the same way it gives validation methods to validate the string and numbers length as well. so in this tutorial i am going to explain how to use min length and max length validation in angular. In angular validation library it provides different…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version