Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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';

@Injectable({
  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';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  s;
  constructor(private myser: MyserviceService) {
   this.s= this.myser.show()
   }

  ngOnInit() {
  }


}

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post