如何在从服务器(nodejs expressjs)获取到角控制器的响应中获取数据?

时间:2021-02-10 09:45:29
$http
   .get('/getFollowings/' + currentUser)
   .success(function(response) {
        $scope.friendlist = response;
   });

I want to get the data which in response.but i cannot handle those values individually. 'response' contain :

我想要得到相应的数据。但我不能单独处理这些值。“响应”包含:

[{"_id":"597c9fabc1ada32277f1da34","following":[{"username":"him"},{"username":"ron"},{"username":"nadu"}]}]

I want to get this usernames.

我想要这个用户名。

4 个解决方案

#1


0  

You can use angular.forEach

您可以使用angular.forEach

$scope.usernames = [];
$http.get('/getFollowings/' + currentUser)
    .success(function(response) {
      $scope.friendlist = response;
      angular.forEach($scope.friendlist[0].following, function(val) {
          $scope.usernames.push(val.username)
      });
    });

Here $scope.usernames is an array with all the values of usernames

这里美元范围。usernames是一个包含所有username值的数组

You can use ng-repeat to display these values in the view.

可以使用ng-repeat在视图中显示这些值。

 var myApp = angular.module('myApp', []);
 myApp.controller('ctrl', ['$scope', function($scope) {
     var response = [{
         "_id": "597c9fabc1ada32277f1da34",
         "following": [{
             "username": "him"
         }, {
             "username": "ron"
         }, {
             "username": "nadu"
         }]
     }];
     $scope.usernames = [];
     angular.forEach(response[0].following, function(val) {
         $scope.usernames.push(val.username)
     });
 }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
<div ng-repeat="username in usernames"><span>{{username}}</span></div>
</div>

#2


2  

I recommend to use lodash library , try this :

我建议使用lodash library,试试这个:

$scope.friendlist = _.chain(response.plain())
                     .map(function(item){
                        item.following = _.pluck(item.following,'username')
                        return item;
                     })
                     .pluck('following')
                     .flatten()
                     .value();

#3


0  

If you are using $http you need to serve Object to client.

如果您正在使用$http,则需要向客户端服务对象。

Structure:

结构:

{
  "data": [
    {
      "_id": "597c9fabc1ada32277f1da34",
      "following": [
        {
          "username": "him"
        },
        {
          "username": "ron"
        },
        {
          "username": "nadu"
        }
      ]
    }
  ]
}

In your controller:

在你的控制器:

$scope.friendlist = response.data;
//i think it's simple
for(var i=0;i<$scope.friendlist.length;i++){
    console.log($scope.friendlist[i]);
}

#4


0  

Just use forEach loop to get all username from the array.

只需使用forEach循环从数组中获取所有用户名。

    $scope.usernameList = [];
    $http
    .get('/getFollowings/' + currentUser)
    .success(function(response) {
       $scope.friendlist = response;           
       $scope.friendlist[0].following.forEach(function(item){
       $scope.usernameList.push(item.username);  
       });
   }); 

So now $scope.usernameList contains all the usernames which are in the array named following.

现在美元范围。usernameList包含数组中命名为following的所有用户名。

#1


0  

You can use angular.forEach

您可以使用angular.forEach

$scope.usernames = [];
$http.get('/getFollowings/' + currentUser)
    .success(function(response) {
      $scope.friendlist = response;
      angular.forEach($scope.friendlist[0].following, function(val) {
          $scope.usernames.push(val.username)
      });
    });

Here $scope.usernames is an array with all the values of usernames

这里美元范围。usernames是一个包含所有username值的数组

You can use ng-repeat to display these values in the view.

可以使用ng-repeat在视图中显示这些值。

 var myApp = angular.module('myApp', []);
 myApp.controller('ctrl', ['$scope', function($scope) {
     var response = [{
         "_id": "597c9fabc1ada32277f1da34",
         "following": [{
             "username": "him"
         }, {
             "username": "ron"
         }, {
             "username": "nadu"
         }]
     }];
     $scope.usernames = [];
     angular.forEach(response[0].following, function(val) {
         $scope.usernames.push(val.username)
     });
 }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
<div ng-repeat="username in usernames"><span>{{username}}</span></div>
</div>

#2


2  

I recommend to use lodash library , try this :

我建议使用lodash library,试试这个:

$scope.friendlist = _.chain(response.plain())
                     .map(function(item){
                        item.following = _.pluck(item.following,'username')
                        return item;
                     })
                     .pluck('following')
                     .flatten()
                     .value();

#3


0  

If you are using $http you need to serve Object to client.

如果您正在使用$http,则需要向客户端服务对象。

Structure:

结构:

{
  "data": [
    {
      "_id": "597c9fabc1ada32277f1da34",
      "following": [
        {
          "username": "him"
        },
        {
          "username": "ron"
        },
        {
          "username": "nadu"
        }
      ]
    }
  ]
}

In your controller:

在你的控制器:

$scope.friendlist = response.data;
//i think it's simple
for(var i=0;i<$scope.friendlist.length;i++){
    console.log($scope.friendlist[i]);
}

#4


0  

Just use forEach loop to get all username from the array.

只需使用forEach循环从数组中获取所有用户名。

    $scope.usernameList = [];
    $http
    .get('/getFollowings/' + currentUser)
    .success(function(response) {
       $scope.friendlist = response;           
       $scope.friendlist[0].following.forEach(function(item){
       $scope.usernameList.push(item.username);  
       });
   }); 

So now $scope.usernameList contains all the usernames which are in the array named following.

现在美元范围。usernameList包含数组中命名为following的所有用户名。