0

Could someone explain me the difference between

bootstrap(MyApp, [provide(Service1, { useClass: Service1})]);

and

bootstrap(MyApp, [Service1]);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
AntuJitsu
  • 119
  • 1
  • 9
  • Note that the Angular docs now discourage us from registering providers using `bootstrap()`. See https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#configuring-the-injector – Mark Rajcok Mar 22 '16 at 19:32

2 Answers2

0

There isn't really a difference.

Using this version

bootstrap(MyApp, [provide(Service1, { useClass: Service1})]);

only makes sense when the classes differ, like

bootstrap(MyApp, [provide(XHRBackend, { useClass: MockBackend})]);

to pass a MockBackend when an XHRBackend is requested (is the type of the constructor parameter.

constructor(private backend:XHRBackend) {}

would get a MockBackend instance passed in.

If you just want to pass Service1 when Service1 is requested

bootstrap(MyApp, [Service1]);

will do.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

It's exactly the same. You can use it when the token used for the provider is the class itself. If you watnt to provide something else behind the provider token, you can use the provide function with useClass, useValue, useExisting or useFactory.

Here are some samples that can interest you:

You could also have a look at the documentation of the Provider class:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360