public baseUrl: string;
public exercises: Exercise[];
constructor(public http: Http, @Inject('BASE_URL') baseUrl: string, ) {
this.baseUrl = baseUrl;
this.http.get(this.baseUrl + 'api/Exercises').subscribe(result => {
this.exercises = result.json() as Exercise[];
console.log(this.exercises.length);//this works(returns nr of objects)
}, error => console.error(error));
console.log(this.exercises.length);//this doesn't work(TypeError: Cannot
//read property 'length' of undefined)
}
出于某种原因,我只能在构造函数完成后在我指定它的位置或在另一个方法(由某个事件触发)中获取长度,因此我假设在构造函数完成后分配全局变量。为什么会这样?有没有办法在get?
之后获取构造函数内部的长度