Karpuz
Karpuz

Reputation: 43

Angular 2 - Injectable() decorator

I don't completely understand the @Injectable decorator in Angular 2. Even though I do not use it, service works. (Look at below examples)

What is the purpose of using @Injectable decorator?

Below are two examples, one with and one without Injectable with respective plunker examples:

With @Injectable Example1 `

@Injectable()
export class AppService{
  get():any[]{
    return "something"
}

Without @Injectable Example2

export class AppService{
  get():any[]{
    return "something"
}

Of course I know the reason of using @Injectable decorator, for Dependency injection, but like i said before, I don't understand completely. Could someone explain?

Upvotes: 2

Views: 370

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658067

Injectable is not required, if the service doesn't have constructor parameters. It's considered to add it to all services anyway, because it's a common mistake that later parameters are added but the @Injectable() decorator is forgotten because it was working before.

Upvotes: 3

Related Questions