Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 and Ionic 3 No provider for HTTP

Tags:

I have a service which imports Http, and when I use it in my app it throws error. "No provider for Http! Error at g injectionError". I am lazyloading the app. Also the provider was generated through cli "ionic g provider ..."

complaint-service.ts

import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map';  @Injectable() export class ComplaintService {     private complaints: {subject: string, description: string}[] = [];      constructor(public http: Http) {         this.http = http;         this.complaints = null;         console.log('Hello ComplaintService Provider');     }      addComplaints(complaint: {subject: string, description: string}) {         this.complaints.push(complaint);     }      getComplaints() {         return this.complaints.slice();     }  } 

complaint-form.ts

import { Component } from '@angular/core'; import {Validators, FormBuilder, FormGroup } from '@angular/forms'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import {ComplaintService} from '../../providers/complaint-service';   @IonicPage() @Component({   selector: 'page-complaint-form',   templateUrl: 'complaint-form.html',   providers: [ComplaintService] }) export class ComplaintForm {  } 

Any suggestions?

like image 280
abhiklpm Avatar asked Apr 25 '17 11:04

abhiklpm


2 Answers

You have to register the HttpModule to your module (/app.module.ts):

import { HttpModule} from '@angular/http';  @NgModule({   imports: [     HttpModule   ],   declarations: [ AppComponent ],   bootstrap:    [ AppComponent ] }) export class AppModule { } 
like image 190
devqon Avatar answered Oct 06 '22 10:10

devqon


Add HTTP to the providers list as well. (to AppModule)

import { HTTP } from '@ionic-native/http'; 

And

  providers: [     ...,     HTTP    ] 

Working with Angulat5+, Ionic 3+

like image 33
Maheswaran Ravisankar Avatar answered Oct 06 '22 08:10

Maheswaran Ravisankar