简体   繁体   English

角回调数组推送问题

[英]Angular callback array push issue

I am trying to create a comments section, similar to that of instagram whereby a list of comments is displayed, along side a user profile and usename for each comment. 我正在尝试创建一个类似于instagram的评论部分,其中显示评论列表,以及每个评论的用户个人资料和用户名。

I have a comments table in parse and a user table in parse, and so I have to perform a further query for each comment to get the profile picture link and username. 我在解析中有一个注释表,在解析中有一个用户表,因此我必须对每个注释执行进一步的查询,以获得个人资料图片链接和用户名。

I am using angular to do this. 我正在使用角度来做到这一点。 I have a comments array to store all the commments, and I am pushing an object onto this array containing: - Username - profile picture - comment 我有一个comment数组来存储所有评论,并且正在将一个对象推到该数组上,其中包含:-用户名-个人资料图片-comment

var commentQuery = parseQuery.new('Comment');
commentQuery.equalTo("ID", id);
parseQuery.find(commentQuery)
.then(function(results) {
    for(var i =0; i < results.length; i++){
        var comment = results[i].get('comment');
        var userQuery = parseQuery.new('_User');
        userQuery.equalTo("objectId", results[i].get('userObject')[0].id);
        parseQuery.find(userQuery)
        .then(function(user) {  
            var commentObject = {
               comment: comment,
               username : '',
               profile: ''
        }
            commentObject['profile'] = user[0].get('profile_picture')['_url'];
            commentObject['username'] = user[0].get('liveUsername');
            $scope.comments.push(commentObject);    
        })
    }   
})

What this then produces in my ng-repeat is a list, with the correct number of comments, however all the comments are the same! 然后在ng-repeat中生成的是一个列表,其中包含正确数量的注释,但是所有注释都是相同的!

If i print out the comments to the console as they are downlaoded, they are correct. 如果我按注释将它们打印到控制台,则它们是正确的。 It is at the object creation where things go wrong, and the same data is placed within all comment objects. 这是在对象创建时出错的地方,并且所有注释对象中都放置了相同的数据。

在此处输入图片说明

$scope.comments contains multiply $scope.commentObject , so when You change $scope.commentObject it changes in whole array. $scope.comments包含乘法$scope.commentObject ,因此,当您更改$scope.commentObject它将在整个数组中更改。 You can create new var in .then(function(user) { callback and push it to array. 您可以在.then(function(user) {回调中创建新的var并将其推送到数组。

@OP The thing is $scope.commentObject is a reference , so when you update this object all its references are updated to the same value . @OP事情是$ scope.commentObject是一个引用,因此更新此对象时,其所有引用都将更新为相同的值。 Consider this example : 考虑这个例子:

var list=[];
$scope.obj="a";
list.push($scope.obj);
console.log(list);  //it will print ['a']
$scope.obj="b";
console.log(list);  //it will print ['b'] , even though  we haven't pushed any new value in the list. 

You see the problem is that you are pushing the same reference again and again. 您会看到问题在于您一次又一次地推送相同的引用。 To resolve it change your code to the following : 要解决此问题,请将您的代码更改为以下内容:

parseQuery.find(userQuery)
    .then(function(user) {  
        var commentObject = {
           comment: comment,
           username : '',
           profile: ''
    }
        commentObject['profile'] = user[0].get('profile_picture')['_url'];
        commentObject['username'] = user[0].get('liveUsername');
        $scope.comments.push(commentObject); 
    })

提供的解决方案是正确的,如果如上所述,您仍然有相同的注释,则应手动检查用户表中的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM