如何在元素下方的AngularJS中推送对象

时间:2015-09-09 01:36:35

标签: javascript angularjs

我有recusativ树...我从laravel api得到它。问题是,当我推送对象时,它不会低于我想要回复的评论,它会结束。

这是我的指示:

elng-repeat中的当前元素,我需要在该元素下添加新元素。

此部分:scope.comments.push(data);会将元素推送到ng-repeat

的末尾
.directive("addreplys", function($compile,commentFactory){
            return {
              link: function (scope, element, attrs) {

                 // I contain the ngModel values for form interaction.
                scope.form = {
                    reply: ""
                };

                //CALL FACTORY TO ADD NEW REPLAY
                scope.addReply = function(el) {

                    commentFactory.saveComment(scope.form.reply[el.pivot.comment_id],el.pivot.project_id,el.pivot.comment_id,el.level+1)
                            .success(function(data){
                                //ADD REPLY TO CLIENT SIDE
                                scope.comments.push(data);
                            })
                            .error(function(data){
                               console.log(data); 
                            });
                    // Reset the form once values have been consumed.
                 scope.form.reply = "";
                };
              }
            };
          });

任何解决方案?

3 个答案:

答案 0 :(得分:2)

推送始终将项目推送到数组的末尾。 使用取消取消而不是推送

好的,然后看: 1.找到索引" idx"数组中的当前文档:

<强> allComments.indexOf(currentDocument)

  1. 将newObject拼接到数组
  2. allComments.splice(idx + 1,0,newObject)

    finalCode

    allComments.splice(allComments.indexOf(currentDocument) + 1, 0, newObject)
    

答案 1 :(得分:0)

你可以使用unshift,元素将在数组的开头:

see here

例如:

  

var a = [1,2,3,4,5];

     

a.unshift(0);

//  a = [0,1,2,3,4,5]

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>


    <div ng-app="myApp" ng-controller="myCtrl">

        <!--Comment section-->
        <ul ng-repeat="comment in comments track by $index" style="background: skyblue; padding: 10px;">
            <li>
                <b>Comment {{$index}} : </b>
                <br>
                {{comment.comment}}
                <!--Reply section-->
                    <ul ng-repeat="reply in comment.reply track by $index">
                        <li><i>Reply {{$index}} :</i><br>
                            {{reply.comment}}</li>
                    </ul>
                <!--End reply section-->
                <input type="text" ng-model="reply" placeholder=" Write your reply." /><a href="" ng-click="insertReply($index,reply)">Reply</a>
            </li>
        </ul>
        <!--End comment section -->
            

        <!--Post your comment-->
        <b>New comment</b>
        <input type="text" placeholder="Your comment" ng-model="comment" />
        <a href="" ng-click="newComment(comment)">Post </a>
    </div>


    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {

            //Comments object having reply oject
            $scope.comments = [{ comment: 'hi', reply: [{ comment: 'hi inside commnet' }, { comment: 'hi inside commnet' }] }];

            //push reply
            $scope.insertReply = function (index, reply) {
                $scope.comments[index].reply.push({ comment: reply });
            }

            //push commnet
            $scope.newComment = function (comment) {
                $scope.comments.push({ comment: comment, reply: [] });
            }
        });
    </script>

</body>
</html>