0

I’m back with another problem.. I have an API on my local server, and I want to communicate with it. But when I do

import {Http, Response} from 'angular2/http'; or  
import { HTTP_PROVIDERS } from 'angular2/http'; or  
import {HTTP_BINDINGS} from 'angular2/http'; or  
import {Http, Response} from 'angular2/http';

I got an error? “404 GET /angular2/http” in the console.

Here is my toolservice.ts :

 import {Injectable} from 'angular2/core';  
 import {Http, Response} from 'angular2/http';  
 import {Tool} from './tool';  
 import {BackendService} from './backend.service';  

 @Injectable()  
 export class ToolService {  
 constructor(  
     private _backend: BackendService /*,http: Http*/ ){ }  

   private _tools:Tool[] = [];  

   getTools() {  
     this._backend.getAll(Tool).then( (tools:Tool[]) => {  
       this._tools.push(...tools); // fill cache  
     });
     return this._tools;   }  
     /*launchtool(){  
     alert("I asked to launch your tool...");  
     http.get("http://localhost:8080/");  
     //this.http.get("http://localhost:8080/");  
    //$http.get("http://localhost:8080/");  
    }*/  
 }

If I uncomment “private _backend: BackendService /, private http: Http/ ){ }“ I got an error cause angular can’t find http.

Did I miss something? I try to do npm install –g http and http-request but it was uneffective..

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Ryusekai
  • 7
  • 4
  • Someone help me. I forgot to import in my main html file angular2/http. http://stackoverflow.com/questions/34748257/angular-2-gives-systemjs-cannot-find-angular2-http-module – Ryusekai Apr 28 '16 at 14:07

1 Answers1

0

This is a services.

import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class BackendService {
    constructor(private _http: Http) {}
getAll():Observable<any> {
return this._http.get('http://localhost:8080/')
.map(res => res.json());
};

Here is component

import { BackendService } from './backend.service';
constructor(private backendservice: BackendService) { }
data: any[];

get(): void {
this.backendservice.getAll().subscribe(data => this.data = data,
error => console.log(error));
}
Pat
  • 116
  • 7