app.module.ts
import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; import {AppComponent} from './app.component'; import {Logging2Service} from './logging2.service'; import {DataService} from './data.service'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule ], providers: [Logging2Service, DataService], bootstrap: [AppComponent] }) export class AppModule { }
logging2.service.ts
import {Injectable} from '@angular/core'; @Injectable({ providedIn: 'root' }) export class Logging2Service { constructor() { } logStatus(status: string) { console.log('Status : ' + status); } }
data.service.ts
import {Injectable} from '@angular/core'; import {Logging2Service} from './logging2.service'; @Injectable({ providedIn: 'root' }) export class DataService { constructor(private loggingService: Logging2Service) { } addNewData(name: string, age: number) { // insert data to db // db.insert(name,number); this.loggingService.logStatus('New'); } }
References
https://github.com/mhdr/AngularSamples/tree/master/019/my-app