Angula2 http.get订阅问题

时间:2016-09-20 12:08:06

标签: angular

订阅者在我的情况下无法正常工作,我的服务是

getProducts() : Observable<Product[]>  {
           return this.http.get("http://ipaddress.com:3000/api/v1/product")
                 .map(this.getResult)
                 .catch(this.handleError);
}

my controller is 

ngOnInit() { 
    this.products   =   this.homeService.getProducts().subscriber(response=>console.log("Hey i am your data"));

}

子网成功回调在我的情况下不起作用

我导入了像这样的库

import { Http, Response, Headers, RequestOptions, Request } from '@angular/http';
import * as Rx from "rxjs/Rx"

2 个答案:

答案 0 :(得分:1)

确保使用JSON处理订户成功。

答案 1 :(得分:0)

使用subscribe代替subscriber

请参阅此工作示例:https://plnkr.co/edit/ANhHASY94iIFzT8BdMnr?p=preview

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {HttpModule, Http} from '@angular/http';

import 'rxjs/Rx';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  constructor(private _http: Http) {
    this.name = 'Angular2'
  }

  ngOnInit() {
    this._http.get('https://httpbin.org/headers')
      .map(resp => resp.json())
      .catch(err => console.log(err))
      .subscribe(
        data => console.log(data)
      );
  }
}

@NgModule({
  imports: [ BrowserModule, HttpModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}