Reputation: 7464
I have been working upon my new Angular2 app and is confused between Injectables and Observables?
Upvotes: 0
Views: 831
Reputation: 657406
These two aren't related in any way. You can use Observables
in Injectables
like anywhere else, but that's about it.
An Observable
is similar what is known as Stream
in other languages.
An Observable
can be subscribed to and when it emits events, subscriber get notified. Observable
is a bit like Promise
but for a possible series of values instead just one value.
Injecable
is a class that can be instantiated and injected by Angulars DI.
(dependency injection).
When a class has a decorator like
@Injectable()
export class SomeClass {
constructor(private http:Http) {}
}
and some other class (for example an Angular2 component) like
@Component({...}
export class MyComponent {
constructor(private someClass:SomeClass) {}
}
that is instantiated by Angulars DI, DI inspects the constructor parameters and looks up injectables that match the constructor parameter declaration, creates a new instance (in this case of SomeClass
), but because SomeClass
also has a constructor parameter, DI first has to look up or create a new instance of Http
to be able to pass it to SomeClass
.
An Injectable
therefore is a class that can be instantiated by DI:
Upvotes: 4