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() {
}
}
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;
}
}
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 Answer of Questions and ASK to Doubt