如何获取用户并将其附加到评论,然后使用 JavaScript 发送结果以查看?

How to get a user and attach it to a review, then send the result to view using JavaScript?

$http.get("api/location/getReviews/" + $routeParams.id) 
.success(function (data) { 
  $scope.reviews = {}; 
  $scope.reviews = data; 
  angular.forEach($scope.reviews, 
    function (value, index) { 
      $http.get("api/user/" + value.UserId) 
      .success(function (user) { 
        $scope.value.User = user; 
      }); 
    }); 
  });

我有以下代码,我想做的是让用户 来自 userId,将其附加到评论,并将其发送到视图中,因为我的评论有一个 userId,我需要用户的电子邮件。当我列出评论时,我想显示电子邮件而不是 id,但我不知道该怎么做。它给我无法读取 属性 或未定义的用户。

谁能解释一下发生了什么?

您可以将循环中 API 调用返回的用户对象放入评论数组中的索引评论对象中,方法如下:

    angular.forEach($scope.reviews, function (value, index) {

            $http.get("api/user/" + value.UserId)
            .success(function (user) {

                  $scope.reviews[index].User = user;

            });

   });