简体   繁体   English

ng-repeat ng-repeat angularJS内部

[英]Ng-repeat inside ng-repeat angularJS

i'm working on a project in angularJS using AJAX and it's a post / comment system with like buttons. 我正在使用AJAX在angularJS中进行项目,这是一个具有类似按钮的发布/评论系统。 Everything is working so far except reading comments from Database which is supposed to be done using a 2nd ng-repeat inside the first one that is reading the posts. 到目前为止,一切工作正常,但要读取数据库中的注释,应该使用第一个读取帖子的ng-repeat来完成。

I can recieve the json with data fine going to the page servicoLeituraComments.php, all data is there. 我可以通过servicoLeituraComments.php页面接收到数据良好的json,所有数据都在那里。 I think the problem is with ng-repeat but I'm not sure how am i suppose to do it when it's inside another, i already tried "comments" or "p.comments" on it and none work. 我认为问题出在ng-repeat上,但是我不确定当它在另一个内时我应该怎么做,我已经在上面尝试过“ comments”或“ p.comments”,但均无用。 Also anything i type inside the 2nd ng-repeat won't appear on page neither. 我在第二次ng-repeat中键入的任何内容也不会显示在页面上。 Here is the code. 这是代码。

  <script>
    var app = angular.module('postsApp', []);
    var interval;

    app.controller('postsCtrl', function($scope) {
        $scope.toggle = false;
        $scope.texto = [];
        $scope.comment = [];
        $scope.comment = "";
        $scope.comments = "";
        $scope.posts = "";
        $scope.texto = "";
        $scope.idPost = 0;
        $scope.showBox = function(p){

          p.toggle = !p.toggle;


          if(interval == 0){
            interval = setInterval("angular.element($('#postsApp')).scope().servicoLeituraPosts()",1000);
          }else{
            clearInterval(interval);
            interval = 0;

          }
           servicoLeituraComments(p);
        };
        $scope.iniciaTimer = function(){
               interval = setInterval("angular.element($('#postsApp')).scope().servicoLeituraPosts()",1000);
        };
        $scope.servicoLeituraPosts = function(){        
                $.getJSON(
                        "servicoLeituraPosts.php",
                        {

                        },
                        function(jsonData)
                        {
                            $scope.posts = jsonData;
                            $scope.$apply();
                        });
        };
        $scope.servicoLeituraComments = function(p){        
                $.getJSON(
                        "servicoLeituraComments.php",
                        {
                            "idPost": p.idPost
                        },
                        function(jsonData)
                        {
                            $scope.comments = jsonData;
                            $scope.$apply();
                        });
        };
        $scope.addPost =  function(){                              
                $.post(
                    "addPostRest.php",
                    {
                        "texto" :  $scope.texto
                    },
                    function(dados)
                    {
                        $scope.texto = dados.indexOf("OK") >= 0 ? "" : "FALHOU";
                        $scope.$apply();
                   }
                );
        };
        $scope.addLike =  function(idPost){
                $.post(
                    "addLike.php",
                    {
                        "idPost" : $scope.idPost = idPost
                    },
                    function(dados)
                    {
                        $scope.texto = dados.indexOf("OK") >= 0 ? "" : "FALHOU";
                        $scope.$apply();
                    }
                );
            };
             $scope.addComment =  function(p){                              
                $.post(
                    "addComentarioRest.php",
                    {

                        "comment" : p.comment,
                        "idPost" : p.idPost
                    },
                    function(dados)
                    {
                        $scope.texto = dados.indexOf("OK") >= 0 ? "" : "FALHOU";
                        $scope.$apply();
                    }
                );
            };      

    });
</script>


<div class="panel panel-default">
        <div class="panel-heading">
            POSTS 
            <a class="btn btn-success pull-right" href="posts.php"><span class="glyphicon glyphicon-refresh"/></a>          
        </div>

        <div class="panel-body">

            <div class="form-group">
                <label for="texto">Texto::</label>

                <textarea ng-model="texto" placeholder="Coloque aqui a mensagem..." class="form-control" rows="5" name="texto"></textarea>
            </div>

            <button ng-click="addPost()" class="btn btn-success btn-xs" type="button">Enviar</button>      

        </div>
</div>

<div class="posts" id="posts">
    <div class='row ng-scope' ng-repeat="p in posts" >
        <div class='col-md-12'>


            {{ p.nome }} -  {{ p.data }} <p><p>  
            {{ p.texto }}                <p><p>
            {{ p.numeroLikes }}  

            <button ng-click="addLike(p.idPost)" class="btn btn-default btn-xs" type="button">Like</button>    

            <span class="abrir_comentario" ng-click="showBox(p)">Comentários</span>

            <div ng-show="p.toggle" id="comentarios">
                <div class="comentarios">



                    <div class="form-group">
                        <textarea ng-model="p.comment" placeholder="Coloque aqui a mensagem..." class="form-control" rows="3" name="texto"></textarea>
                    </div>


                    <p><p><p><button ng-click="addComment(p)" class="btn btn-success btn-xs" type="button">Enviar</button>

                    <div class="comments" id="comments">
                        <div class='row ng-scope' ng-repeat="c in p.comments" >   
                            <div class='col-md-12'>

                                  {{ c.nome }} -  {{ c.data }} <p><p>  
                                  {{ c.texto }}                <p><p>
                            </div>
                        </div>
                    </div>


                </div>
            </div> <p>


        </div>
    </div>
</div>  

Here is JSon array from servicoLeituraPosts.php 这是来自servicoLeituraPosts.php的JSon数组

[
{
    "idPost":"12",
            "data":"2017-06-21 01:17:05",
            "nome":"joao",
            "texto":"Ola",
            "idAutor":"3",
            "numeroLikes":"3"
    },
    {
    "idPost":"13",
            "data":"2017-06-21 01:24:10",
    "nome":"joao",
            "texto":"Eu sou o joao",
            "idAutor":"3",
            "numeroLikes":"3"
}

] ]

And here is JSon array from servicoLeituraComments.php 这是来自servicoLeituraComments.php的JSon数组

[
{
    "nome":"joao",
    "texto":"12345",
    "data":null},
    {
    "nome":"joao",
    "texto":"1234",
    "data":null
}

] ]

So there are two things I am seeing here. 所以我在这里看到两件事。 The first is that the JSON you are trying to get comments from doesn't have a comments property. 首先是您要从中获取注释的JSON没有注释属性。 If it did it would be like this: 如果这样做,它将是这样的:

{
        "idPost":"12",
        "data":"2017-06-21 01:17:05",
        "nome":"joao",
        "texto":"Ola",
        "idAutor":"3",
        "numeroLikes":"3"
        "comments": []  //This is missing, these would be p.comments
}

The second thing I see is that you have a <textarea> with an ng-model="p.comments" . 我看到的第二件事是您有一个带有ng-model="p.comments"<textarea> Are you trying to use this to add comments to $scope.posts ? 您是否要使用它向$scope.posts添加注释? If so you should change that model to something like ng-model="newComment" and addComment() should find $scope.newComment and push it to $scope.posts 如果是这样,您应该将该模型更改为类似ng-model="newComment"并且addComment()应该找到$scope.newComment并将其推送到$scope.posts

Try this: 尝试这个:

ng-click="addComment($index)"

$scope.addComment = function(index){
  $scope.posts[index].comments.push($scope.newComment);
  $scope.newComment = '';
}

Edit 编辑

It doesn't matter if you get the posts in one JSON, and the comments in another. 是否以一种JSON形式获取帖子,以另一种形式获取评论都无关紧要。 The only problem I can see is the way your comments JSON is. 我能看到的唯一问题是注释JSON的方式。 There would need to be another field for the comments to know which posts to attach themselves to. 评论将需要有另一个字段,以了解要附加到哪些帖子。 Like this: 像这样:

{
    "nome":"joao",
    "texto":"12345",
    "data":null,
    "idPost": "12"  //This is how you would know that this comment goes to this post
}

谢谢大家,我设法解决了这个问题,我将两个ng-repeats分别放在另一个内部,因为我有2个JSON,它们的数据通过ID相互关联。

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

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