试图找出如何进行二次通话

时间:2018-04-16 02:36:35

标签: angular

我有一个 Angular 控件,我从API端点调用一个函数。我试图找出如何接受该调用的结果并在后续调用中使用其中的值。

例如,我打电话给用户信息。返回后,我想使用用户的年龄或身高进行第二次API调用...

所以我在@OnInit

中调用它
ngOnInit(){
    this.auth.memberInfo()
        .subscribe(res =>{
            this.user = res.json();
    })
}

在AngularJS中,我们有.then()函数,我们将在第一个过程完成后用于后续操作。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

当你接受我的评论作为正确答案时,我是这个答案的创造者,所以其他人也可以参考

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

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

答案 1 :(得分:0)

没有什么能阻止你在第一个订阅方法中进行另一个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
        })
      })
  }
相关问题