angular 2 中的订阅功能

subscribe function in angular 2

我想在相同的订阅方法或 ngOninit 方法(评论一个)中查看从服务返回的结果,但无论是在显示结果的地方还是从服务获取结果的地方..

ngOnInit() {
    this.cityAreaService.getCities().subscribe(data => {
        this.cities = data;
        //console.log(this.cities);
    });
    //console.log(this.cities) 
}

服务

getCities() {
    return this.http.get(globalVar.serviceUrl + 'Cities').map((res: Response) => res.json());
}

您可以尝试在订阅的其他块中读取响应

this.cityAreaService.getCities().subscribe(
  response => {
    this.cities = response;
  },
  error => {
    // maybe error
  },
  () => {
    // anything else?
  }
);