I don't understand When to use @Inject and when to use @Injectable ?
import {Component, Inject, provide} from '@angular/core';
import {Hamburger} from '../services/hamburger';
export class App {
bunType: string;
constructor(@Inject(Hamburger) h) {
this.bunType = h.bun.type;
}
}
And..
import {Injectable} from '@angular/core';
import {Bun} from './bun';
@Injectable()
export class Hamburger {
constructor(public bun: Bun) {
}
}
The @Injectable
decorator aims to actually set some metadata about which dependencies to inject into the constructor of the associated class. It's a class decorator that doesn't require parameters. Without this decorator no dependency will be injected...
@Injectable()
export class SomeService {
constructor(private http:Http) {
}
}
The @Inject
decorator must be used at the level of constructor parameters to specify metadata regarding elements to inject. Without it, the type of parameters is used (obj:SomeType
is equivalent to @Inject(SomeType) obj
).
@Injectable()
export class SomeService {
constructor(@Inject(Http) private http:Http, @Inject('sometoken') obj) {
}
}