Services in Angular Framework

Services in Angular Framework:-

It is used to write the common code of an application that will be implemented into multiple components. for example, if we want to create a notification of an application then we will create a separate service for notification and inject service into the component.

 ng g service myservice

Service should be import into app.module.ts 

write under providers attribute 

implement logic under Service Class and Write Functionality
import { Injectable } from '@angular/core';

  providedIn: 'root'
export class MyserviceService {

  constructor() { }
  show() { 
      let ndate = new Date(); 
      return ndate; 


Call Functionality under component after injection

import { Component, OnInit } from '@angular/core';
import { MyserviceService } from '../myservice.service';
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
export class HomeComponent implements OnInit {
  constructor(private myser: MyserviceService) {

  ngOnInit() {


Post a Comment


POST Answer of Questions and ASK to Doubt

Post a Comment (0)