Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Angular 想知道怎么打第二个电话吗_Angular - Fatal编程技术网

Angular 想知道怎么打第二个电话吗

Angular 想知道怎么打第二个电话吗,angular,Angular,我有一个角度控制,从API端点调用一个函数。我试图弄清楚如何获取该调用的结果,并在后续调用中使用其中的值 例如,我打电话询问用户信息。当返回该get时,我想使用用户的年龄或身高进行第二次API调用 所以我在@OnInit ngOnInit(){ this.auth.memberInfo() .subscribe(res =>{ this.user = res.json(); }) } 在AngularJS中,我们有.then()函数

我有一个角度控制,从API端点调用一个函数。我试图弄清楚如何获取该调用的结果,并在后续调用中使用其中的值

例如,我打电话询问用户信息。当返回该get时,我想使用用户的年龄或身高进行第二次API调用

所以我在
@OnInit

ngOnInit(){
    this.auth.memberInfo()
        .subscribe(res =>{
            this.user = res.json();
    })
}
在AngularJS中,我们有
.then()
函数,在第一个过程完成后,我们将使用该函数执行后续操作


我如何才能做到这一点?

在第一个subscribe方法中,没有任何东西可以阻止您执行另一个API调用

  ngOnInit(){
    this.auth.memberInfo()
      .subscribe(res => {
        this.user = res.json();
        // example API call
        this.http.get(`/api/user/age/{this.user.age}`).subscribe(result => {
        // do what you need to do with result here
        })
      })
  }

由于你接受了我的评论作为正确答案,我创造了这个答案,这样其他人也可以参考

  ngOnInit(){
    this.auth.memberInfo()
      .subscribe(res => {
        this.user = res.json();
        this.getDetails(this.user.age);
      })
  }

  getDetails(age : number){
       this.auth.WhateverTheMethod().subscribe(result => {
        // result
        })
   }

为什么不在subscribe()中调用该方法?是的。成功了。被卡住是件愚蠢的事。谢谢!!