Scipion
Scipion

Reputation: 11888

Angular2 Adding providers dependency inside main.ts or component itself

I have a very simple app in angular2 with three files:

main.ts
app -|
      app.component.ts
      app.service.ts

In my main.ts I have tried both these things:

import { bootstrap } from "angular2/platform/browser"
import "rxjs/add/operator/map"

import { AppComponent} from "./app/app.component"

bootstrap(AppComponent)

and

import { bootstrap } from "angular2/platform/browser"
import { HTTP_PROVIDERS } from "angular2/http"
import "rxjs/add/operator/map"

import { AppComponent} from "./app/app.component"

bootstrap(AppComponent, [HTTP_PROVIDERS])

In the first case, since I haven't injected HTTP_PROVIDERS, I need to do in my app.component.ts:

/*Imports*/
@Component({
    selector: "my-app",
    templateUrl: "app/app.template.html",
    providers: [
        HTTP_PROVIDERS,
        AppService
    ]
})
/*Export*/

Is it the same to have injection (here HTTP_PROVIDERS) in the Component or in the boostrap ?

If so, what is the best practise ?

Upvotes: 1

Views: 3193

Answers (1)

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

Reputation: 657741

It is the same. According to the style guide prefer adding providers to providers: [...] of the root component instead of bootstrap()

See also

Upvotes: 3

Related Questions