Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将AngularJS中的对象推到元素的正下方_Javascript_Angularjs - Fatal编程技术网

Javascript 如何将AngularJS中的对象推到元素的正下方

Javascript 如何将AngularJS中的对象推到元素的正下方,javascript,angularjs,Javascript,Angularjs,我有一棵树。。。我是从laravel api得到的。问题是,当我推对象时,它不会在我想要回复的评论下面,而是在末尾 这是我的指示: el是ng repeat中的当前元素,我需要在该元素下方添加新元素 这部分:scope.comments.push(数据)将推动ng repeat .directive("addreplys", function($compile,commentFactory){ return { link: function (

我有一棵树。。。我是从laravel api得到的。问题是,当我推对象时,它不会在我想要回复的评论下面,而是在末尾

这是我的指示:

el
ng repeat
中的当前元素,我需要在该元素下方添加新元素

这部分:
scope.comments.push(数据)将推动
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 = "";
                };
              }
            };
          });

任何解决方案?

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

好的,然后看: 1.在数组中查找当前文档的索引“idx”:

allComments.indexOf(currentDocument)

  • 将新对象拼接到数组中
  • allComments.splice(idx+1,0,newObject)

    最终代码

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

    您可以使用unshift,元素将位于数组的开头:

    例:

    变量a=[1,2,3,4,5]

    a、 取消移位(0)

    
    
    • 注释{{$index}}:
      {{comment.comment}
      • 答复{{$index}:
        {{reply.comment}
    新评论 var-app=angular.module('myApp',[]); 应用程序控制器('myCtrl',函数($scope){ //具有回复对象的Comments对象 $scope.comments=[{comment:'hi',回复:[{comment:'hi inside commnet'},{comment:'hi inside commnet'}]}]; //推送回复 $scope.insertReply=函数(索引,回复){ $scope.comments[index].reply.push({comment:reply}); } //推送通信网 $scope.newComment=函数(注释){ $scope.comments.push({comment:comment,reply:[]}); } });
    你是说回复评论吗?是的。。。。我的代码有问题吗?它会将我的对象放在所有注释的开头,而不是当前注释的下面。。。
    //  a = [0,1,2,3,4,5]