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.No answers yet.
Log in to leave an answer
Blogs
Clean Code in Rust: Principles and Practical Examples
How to Check if an Array Contains a Value in Java: 5 Fatal Mistakes
Ternary Operator in Kotlin: 5 Main Mistakes Beginners Make
Instantiate and Present ViewController in Swift: Complete Guide with Answers to Common Questions