繁体   English   中英

ng-repeat ng-repeat angularJS内部

[英]Ng-repeat inside ng-repeat angularJS

我正在使用AJAX在angularJS中进行项目,这是一个具有类似按钮的发布/评论系统。 到目前为止,一切工作正常,但要读取数据库中的注释,应该使用第一个读取帖子的ng-repeat来完成。

我可以通过servicoLeituraComments.php页面接收到数据良好的json,所有数据都在那里。 我认为问题出在ng-repeat上,但是我不确定当它在另一个内时我应该怎么做,我已经在上面尝试过“ comments”或“ p.comments”,但均无用。 我在第二次ng-repeat中键入的任何内容也不会显示在页面上。 这是代码。

  <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>  

这是来自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"
}

]

这是来自servicoLeituraComments.php的JSon数组

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

]

所以我在这里看到两件事。 首先是您要从中获取注释的JSON没有注释属性。 如果这样做,它将是这样的:

{
        "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
}

我看到的第二件事是您有一个带有ng-model="p.comments"<textarea> 您是否要使用它向$scope.posts添加注释? 如果是这样,您应该将该模型更改为类似ng-model="newComment"并且addComment()应该找到$scope.newComment并将其推送到$scope.posts

尝试这个:

ng-click="addComment($index)"

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

编辑

是否以一种JSON形式获取帖子,以另一种形式获取评论都无关紧要。 我能看到的唯一问题是注释JSON的方式。 评论将需要有另一个字段,以了解要附加到哪些帖子。 像这样:

{
    "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