Avatar ·

Angular: Error "No provider for NameService" — what to do?

Hello everyone! I'm new to Angular (version 2+), trying to understand services and dependency injection. I have a simple service `NameService` that I want to use in a component to display the user's name. Here is my service (`name.service.ts`): ```typescript export class NameService { getName(): string { return 'Ivan Ivanov'; } } ``` Here is my component (`app.component.ts`): ```typescript import { Component } from '@angular/core'; import { NameService } from './name.service'; @Component({ selector: 'app-root', template: `

Hello, {{ name }}!

` }) export class AppComponent { name: string; constructor(private nameService: NameService) { this.name = this.nameService.getName(); } } ``` When I run the application, the browser console shows an error: `Error: No provider for NameService!` I thought that if I import the service and specify it in the constructor, Angular would pick it up automatically. But apparently, I am missing something.