Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为http get方法返回的变量赋值?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何为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

如何为http get方法返回的变量赋值? 我在构造函数中声明了一个变量this.artists=null。我想将$http.get返回的值分配给
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就不存在了。 你有两个选择

  • async/await-您可能希望在后台完成此任务,因此async/await并不理想

  • 在Then块中调用set方法

  • e、 g

    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不存在。 你有两个选择

  • async/await-您可能希望在后台完成此任务,因此async/await并不理想

  • 在Then块中调用set方法

  • e、 g

    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
    }