In Angular2, you have the following provide definition, which allows you to setup different kinds of dependencies:
provide(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}
Comparing to Angular 1
app.service
in Angular1 is equivalent to useClass
in Angular2.
app.factory
in Angular1 is equivalent to useFactory
in Angular2.
app.constant
and app.value
has been simplified to useValue
with less constraints. i.e. there is no config
block anymore.
app.provider
- There is no equivalent in Angular 2.
Examples
To setup with the root injector:
bootstrap(AppComponent,[provide(API_ENDPOINT, { useValue='http://127.0.0.1:6666/api/' })]);
Or setup with your component's injector:
providers: [provide(API_ENDPOINT, { useValue: 'http://127.0.0.1:6666/api/'})]
provide
is short hand for:
var injectorValue = Injector.resolveAndCreate([
new Provider(API_ENDPOINT, { useValue: 'http://127.0.0.1:6666/api/'})
]);
With the injector, getting the value is easy:
var endpoint = injectorValue.get(API_ENDPOINT);