我无法从$ scope.notifications = response .data获取详细信息

时间:2017-06-23 13:08:53

标签: angularjs node.js

您好我有这个代码并且工作正常,我想从 response.data 获取用户详细信息的详细信息。 response.data 中有几个对象。 userId 就是其中之一,我想获取与“ userId ”关联的用户的详细信息 有人可以帮我从response.data

中提取 userId 的详细信息
$scope.notifications = [];
    $scope.getNotifications = function(){
        $http.get(baseUrl+'')
        .success(function (response) {
            $scope.notifications = response.data;
            alert($scope.notifications.userId);
        }).error(function(data, status){
            //$scope.isSending = false;
        });
    };

This is detals inside my response.data

2 个答案:

答案 0 :(得分:0)

如果您想要使用HTML访问,则应该执行 setTimeout() ;如果要在控制器中访问,则应使用 ng-repeat 。< / p>

angular.forEach

修改 假设您有$ scope.userID,然后您可以使用以下行获取UserDetails,

angular.forEach($scope.notifications, function(value, key) {
  console.log(value.userId);
});

<强>样本

&#13;
&#13;
$scope.userDetails = $scope.notifications.filter(o=> o.userid == $scope.userID);
&#13;
var app = angular.module('plunker',[])
app.controller('MainCtrl',function($scope){
$scope.userID = '9e4e5627-8504-4035-9037-c56f9b5920cd';
   $scope.notifications = [{
  "id": 1,
  "commentid": "Otto",
  "groupid": 1,
  "postid": "osawbridge0@mysql.com",
  "status": "Male",
  "type": "Administrative Officer",
  "userid": "9e4e5627-8504-4035-9037-c56f9b5920cd"
}, {
  "id": 2,
  "commentid": "Sayer",
  "groupid": 2,
  "postid": "ssowley1@lulu.com",
  "status": "Male",
  "type": "Chemical Engineer",
  "userid": "0f07bc0e-1385-4bb8-ac4b-9262039536f7"
}, {
  "id": 3,
  "commentid": "Analiese",
  "groupid": 3,
  "postid": "amabley2@umich.edu",
  "status": "Female",
  "type": "VP Sales",
  "userid": "28552fd8-1fb7-42e0-a0c3-d32f05389d25"
}];

$scope.userDetails = $scope.notifications.filter(o=> o.userid == $scope.userID);

});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用javascript filter方法

var userID = $scope.notifications.filter(o=>{
  if(o.hasOwnProperty("userId")){
    return o.userId;
  }
})