在angular2中无法在subscribe之外访问的数据

时间:2017-03-02 10:07:36

标签: angular

我有一个包含一些数据的json文件,使用HTTP Get请求来获取文件。

这是我的服务。

import 'rxjs/Rx'
import { HTTP } from '@angular/http'
import {Injectable} from '@angular/core'
@Injectable

export class service{
constructor(private http: HTTP){
getDesignation(){
return this.http.get('URL path').map((res: Responce) => res.json());
}
}
}

这是我使用我的服务的组件。

 import { service } from './service'

export class Compo1{

 public designation; any[] = [];

constructor(private httpService: service){
this.httpService().subscribe((data => {
const myarray = [];
for(let key in data){
myArray.push(data[key]);
}
 this.designation = myArray;
console.log(this.designation) ///// This works I can see data in     console....
});
console.log(this.designation) //// This does not work.....
}
}

0 个答案:

没有答案