Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Welcome to my Angular course! If you’ve been following along, you know that we’ve covered directives, including attribute directives and structural directives.
We’ve even created our own custom directives! Now, it’s time to move on to a key concept in Angular that will simplify your app development: Services.
In simple terms, services in Angular are like a central repository for reusable logic. Think of them as a way to avoid repeating yourself when writing code.
Instead of writing the same functionality across multiple components, services allow you to define that logic once and reuse it wherever needed. This approach makes your code cleaner and easier to maintain.
Imagine this scenario: you’re developing a large Angular app. Every time you add a new user or change a name, you need to log some data. You could write console.log
statements in multiple places.
But what happens if you want to change how data is logged? You’d have to go through each component and modify the logic in every location.
This is where services come in handy. Instead of duplicating the same code everywhere, you can create a service that contains the logic and call it whenever needed.
Let’s break it down with an example.
In your Angular app, say you want to log a message whenever a user is added or a name is changed. Here’s how you might initially approach it:
// Inside your component
addUser() {
console.log("User is added");
// Additional logic...
}
changeName() {
console.log("Name is changed");
// Additional logic...
}
This works fine for now. But what if the logging logic becomes more complex? What if you want to log to a database or an external service instead of just the console? You would have to modify both the addUser
and changeName
methods—and anywhere else you’re logging data. This quickly becomes tedious and error-prone.
With Angular services, you can centralize this logging logic in one place. Here’s how you can create a logging service to manage your logging.
ng generate service logging
This will create a logging.service.ts
file. Inside that file, define a class that handles the logging:
export class LoggingService {
logToConsole(status: string) {
console.log(status);
}
}
import { LoggingService } from './logging.service';
export class UserComponent {
constructor(private loggingService: LoggingService) {}
addUser() {
this.loggingService.logToConsole("User is added");
// Additional logic...
}
changeName() {
this.loggingService.logToConsole("Name is changed");
// Additional logic...
}
}
Now, whenever you need to log something, you simply call the service’s logToConsole
method. If the logging logic needs to change, you only need to update the service, and the change will be applied everywhere it’s used.
Although creating a service and manually importing it into your components works, Angular offers a more powerful and elegant solution: Dependency Injection (DI).
DI allows Angular to automatically provide services to your components without you needing to instantiate them yourself.
This approach improves efficiency and makes your app more scalable.
We’ll dive deeper into dependency injection in the next post, where I’ll show you how to implement services the right way in Angular.
In this blog post, we’ve explored the basics of Angular services, why they’re useful, and how to create one. To recap: