Search code examples
angularangular2-services

Angular 2 Beta - issue while Injecting http data service to component


I am new to Angular2 , with Angular 1.3 experience I am trying to build a large Angular 2 app to get the respective of how each features are wired (Template syntax , DI , Routing , Observable). Actually the problem I am facing is while trying to create a separate data service and put all the HTTP calls and inject it in other components.Below code I get this error EXCEPTION: No provider for ConnectionBackend!

Also I need help removing the redundant imports in dashboard summary.

DashboardHTTPSevice

import {Http, Response} from 'angular2/http'
import {Injectable} from 'angular2/core'

@Injectable()
export class DashboardHTTPSevice {

    http:Http;
    constructor(http:Http){
        this.http = http;
    }

    getSummary(){
        return this.http.get('api/dashboardSummary').map((res: Response) => res.json());
    }

}

DashboardSummary

import {Component} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/Rx' ;
import {DashboardHTTPSevice} from 'app/services/dashboardHTTPSevice';


@Component({
    selector: 'dashboard-summary',
    templateUrl: './app/admin/dashboard/summary/summary.component.html',
    styleUrls: ['./app/admin/dashboard/summary/summary.style.css'],
    viewProviders: [HTTP_PROVIDERS],
    providers : [DashboardHTTPSevice]

})

export class SummaryComponent { 

    public dashboardSummary;
    constructor (public http: Http , dataservice : DashboardHTTPSevice){

    dataservice.getSummary().
            .subscribe(summary => {
                console.log(summary.data);
                this.dashboardSummary = summary.data;
            });        
    }
}

Bootstrap

import {bootstrap}    from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {DashboardHTTPSevice} from 'app/services/dashboardHTTPSevice';


bootstrap(AppComponent , ROUTER_PROVIDERS , HTTP_PROVIDERS,DashboardHTTPSevice);

Source : https://github.com/sabha/angular2_store

Demo : http://demoapp-angular2store.rhcloud.com/client/index.html


Solution

  • Put ROUTER_PROVIDERS, HTTP_PROVIDERS and DashboardHTTPService into array.

    bootstrap(AppComponent , [ROUTER_PROVIDERS , HTTP_PROVIDERS, DashboardHTTPSevice]);
    

    See signature of bootstrap function here: