0

I am trying to read json file data to my Html page, but getting 'No provider for ConnectionBackend!

See below code

Home.ts

import { Component } from '@angular/core';
import { NavController ,Platform} from 'ionic-angular';
import { SampleDataService } from '../../providers/sample-data-service';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

  myjsondata:any;

  constructor(public navCtrl: NavController, platform: Platform,
  sampleDataService:SampleDataService) {
                sampleDataService.getJsonData().subscribe((data) => {
                console.log("what is in the data ", data);
                this.myjsondata = data;
              });    
  }  

  ngOnInit(){}
}

app.component.ts

import { SampleDataService } from '../providers/sample-data-service';
import { Http } from '@angular/http';

@Component({
  templateUrl: 'app.html',
  providers:[SampleDataService,Http]

})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = HomePage;

  pages: Array<{title: string, component: any}>;

  constructor(public platform: Platform, public statusBar: StatusBar,
     public splashScreen: SplashScreen, sampleDataService:SampleDataService,
     http:Http) {
    statusBar.styleDefault();
    sampleDataService.getJsonData();

    this.initializeApp();

app.module.ts

import { SampleDataService } from '../providers/sample-data-service';
import { Http} from '@angular/http';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    ListPage,
    About,
    Contact,
    AboutCity
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
    //AutoCompleteModule,
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    ListPage,
    About,
    Contact,
    AboutCity
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    SampleDataService,
    Http
  ]
})

I tried a lot but not getting solution why this error is. Can please anyone help me.

Sam
  • 121
  • 1
  • 1
  • 6

1 Answers1

1

You have to import HttpModule in the imports section in the app.module.ts Also, remove the Http from the providers.

import { SampleDataService } from '../providers/sample-data-service';
import { HttpModule} from '@angular/http';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    ListPage,
    About,
    Contact,
    AboutCity
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
    //AutoCompleteModule,
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    ListPage,
    About,
    Contact,
    AboutCity
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    SampleDataService,

  ]
})

Please see this highly voted answer as well.

Community
  • 1
  • 1
Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52