首页 > 解决方案 > AngularJS - 在承诺之间共享返回值

问题描述

我正在向我的服务器发出get 请求,我得到响应并将值存储在$scope.productId 中

  userService.get(true)
    .then(function(res) {
      $scope.productId = res.user.productid;
      }
    });

然后我需要在对 api 的另一个get 请求中使用这个值来获取与这个 id 相关的产品。

  apiService.get('/product/' + ???)

  .then(function(response) {
    console.log(response)
  })

  .catch(function(response) {
  });

我是新的承诺,所以目标是将第一个请求的价值传递给第二个!

标签: angularjshttpget

解决方案


用这个

userService.get(true)
    .then(function(res) {
      $scope.productId = res.user.productid;
      apiService.get('/product/' + ???)

      .then(function(response) {
        console.log(response)
      })

      .catch(function(response) {
      });
    }
  });

推荐阅读