Javascript 如何为http get方法返回的变量赋值?
如何为http get方法返回的变量赋值? 我在构造函数中声明了一个变量this.artists=null。我想将$http.get返回的值分配给Javascript 如何为http get方法返回的变量赋值?,javascript,angularjs,Javascript,Angularjs,如何为http get方法返回的变量赋值? 我在构造函数中声明了一个变量this.artists=null。我想将$http.get返回的值分配给this.artists变量。 Res.datareturn me对象-可以,但我无法将其分配给变量this.artists export default class ArtistsService { constructor($http) { 'ngInject'; this.$http = $http; this.arti
this.artists变量
。
Res.data
return me对象-可以,但我无法将其分配给变量this.artists
export default class ArtistsService {
constructor($http) {
'ngInject';
this.$http = $http;
this.artists = null;
}
getArtists() {
return this.$http.get("https://api.spotify.com/v1/search?
q=Muse&type=track%2Cartist&market=US&limit=10&offset=5",
{headers: {
'Authorization': <Authorization>}, params
}).then(function mySuccess(res) {
this.artists = res.data;
console.log(this.artists);
}).catch( function myError(res) {
});
};
}
导出默认类ArtistsService{
构造函数($http){
"ngInject",;
这个。$http=$http;
this.artists=null;
}
getArtists(){
返回此。$http.get(“https://api.spotify.com/v1/search?
q=Muse&type=track%2Cartist&market=US&limit=10&offset=5“,
{标题:{
“授权”:},参数
}).then(函数mySuccess(res){
this.artists=res.data;
console.log(this.artists);
}).catch(函数myError(res){
});
};
}
看一看
在showClosureExample
方法中,请注意bound
方法是如何使用的。胖箭头使其函数作用域词法
y绑定到其父上下文,其中as作用域
绑定到自身(作用域
)。
当我们在scoped
中引用this
时,我们得到函数的上下文,当我们在bound
中记录this
时,我们得到Foo
的上下文(this.Foo
)
class-Foo{
构造函数(){
this.foo=null
this.showClosureExample()
}
showclosure示例(){
const scoped=函数(){
console.log(这个)
}
常数界=()=>{
console.log(这个)
}
作用域()//未定义日志
bound()//记录Foo的属性
}
}
新建Foo()
请查看
在showClosureExample
方法中,请注意bound
方法是如何使用的。胖箭头使其函数作用域词法
y绑定到其父上下文,其中as作用域
绑定到自身(作用域
)。
当我们在scoped
中引用this
时,我们得到函数的上下文,当我们在bound
中记录this
时,我们得到Foo
的上下文(this.Foo
)
class-Foo{
构造函数(){
this.foo=null
this.showClosureExample()
}
showclosure示例(){
const scoped=函数(){
console.log(这个)
}
常数界=()=>{
console.log(这个)
}
作用域()//未定义日志
bound()//记录Foo的属性
}
}
新的Foo()
在你关闭之后,这个.artists就不存在了。
你有两个选择
getArtists(){
这是.$http.get(“https://api.spotify.com/v1/search?
q=Muse&type=track%2Cartist&market=US&limit=10&offset=5“,
{标题:{
“授权”:},参数
}).then(函数mySuccess(res){
//this.artists=res.data;
setArtists(res.data);
}).catch(函数myError(res){
});
};
}
艺术家(艺术家){
这就是艺术家
}
在你然后关闭时,这个.artists不存在。
你有两个选择
getArtists(){
这是.$http.get(“https://api.spotify.com/v1/search?
q=Muse&type=track%2Cartist&market=US&limit=10&offset=5“,
{标题:{
“授权”:},参数
}).then(函数mySuccess(res){
//this.artists=res.data;
setArtists(res.data);
}).catch(函数myError(res){
});
};
}
艺术家(艺术家){
这就是艺术家
}
getArtists() {
this.$http.get("https://api.spotify.com/v1/search?
q=Muse&type=track%2Cartist&market=US&limit=10&offset=5",
{headers: {
'Authorization': <Authorization>}, params
}).then(function mySuccess(res) {
//this.artists = res.data;
setArtists(res.data);
}).catch( function myError(res) {
});
};
}
setArtists(artists) {
this.artists = artists
}