Как уже упоминалось @Sajeetharan, вам придется вызывать службу из компонента.
Извлечь этот POC стека, который также показывает, как импортировать созданную службу в ваш модуль передВы можете добавить его в компонент.
Сервис
import { Injectable } from '@angular/core';
@Injectable()
export class Service {
public getData(): string {
return "Data From Service";
}
}
Модуль
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { Service } from './service'
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HelloComponent ],
providers: [Service],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Компонент
import { Component } from '@angular/core';
import { Service } from './service'
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
public data: string;
constructor(private _Service: Service) {
}
public getData(): void {
this.data = this._Service.getData();
}
}
HTML
<hello name="{{ name }}"></hello>
<button (click)="getData()">get data</button>
<p> data from service - {{data}} </p>