订阅角2中的功能

问题描述:

我想在同一订阅方法或ngOninit方法(注释一)中看到从服务返回的结果,但它没有显示结果,即使它正在从服务中获取结果..订阅角2中的功能

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()); 
} 
+0

您的服务如何? – Sajeetharan

+0

显示您的服务。如果取得服务结果,您应该能够在代码中使用第一个console.log查看结果。 – porgo

+0

尝试在订阅记录错误..调试它 –

你可以尝试读取的其他块响应订阅

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

OP和你的代码没有区别。 –