我正在使用angular2。在我的项目中,使用http.get()方法获取json内容并将其分配给一个变量。我想在构造函数之外访问这些变量值。我怎样才能使它成为可能?如何在构造函数外访问构造函数的值
在我用我的组件页面..
public result;
constructor(private http : Http){
this.http.get('http://192.168.0.100:8000/json1')
.map(response => response.json())
.subscribe(data =>{ this.result = data});
}
// I want to acces this.result outside the constructor and assigned to a public variable
public b = JSON.stringify(this.result);
// but it is not working
我如何可以访问此? 在此先感谢
你不知道需要多长时间来执行这个和变量可用时,请将请求移至某个方法并在需要时调用该方法。 – Alex