【发布时间】:2020-12-17 10:27:05
【问题描述】:
我尝试获取 URL 的内容。因为我处于异步模式,所以我使用 Promise。
使用 function(results) 我可以在 test123 变量中得到我想要的一切。但是,当我尝试将该结果存储在 test1234 变量中以在函数之外使用它时,它不起作用。更准确地说,test1234 的内容变得未定义..
我该怎么做才能使 test1234 变量充满该 http.get 的内容?
这里是sn-p:
this.http.get(URL2).toPromise().then(
function(results) {
var test123 = results['features'][0]['properties']['gid']; // works
this.test1234 = test123;
console.log(this.test1234); // doesn't work, it's "undefined"
},
//error function
function(err) {
//handle error
}
);
感谢您的帮助。
【问题讨论】:
-
看起来
test123确实未定义,我会检查这部分results['features'][0]['properties']['gid']。另外你为什么不使用 Observables? -
你为什么在 Angular 中使用 Promise?你怎么知道那东西有效?无论如何,使用箭头功能。
标签: angular asynchronous ionic-framework promise get