简体   繁体   English

ng-repeat的AngularJS问题并将其保存在内部

[英]AngularJS issue with ng-repeat and save input inside

I'am currently trying to make something similar to facebook post/likes/comment system, so far I have managed to do posts and likes, but I'm having issues with comments. 我目前正在尝试制作与facebook帖子/喜欢/评论系统类似的内容,到目前为止,我已经成功地进行了帖子和喜欢,但是我在评论方面遇到了问题。

This is the controller code and the html to show the stuff. 这是控制器代码和显示内容的html。 The problem is when I'm trying to get input from the 2nd textarea with ng-model="texto", where it should save the comment and save on scope, but seems it's not working. 问题是当我尝试使用ng-model =“ texto”从第二个textarea获取输入时,它应该保存注释并保存作用域,但似乎不起作用。 I'm still novice when it comes to angular and how ng-repeat works. 关于角度以及ng-repeat的工作原理,我还是新手。

Also it is sending information to database, idPost and user ID, but the text is plain blank. 它还向数据库,idPost和用户ID发送信息,但文本为纯空白。

EDIT I removed the HTTP post part and all database requests are done in AJAX using timer. 编辑我删除了HTTP发布部分,所有数据库请求都使用计时器在AJAX中完成。 The problem with comments being sent to database still persists. 将注释发送到数据库的问题仍然存在。

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

    app.controller('postsCtrl', function($scope) {
        $scope.toggle = false;
        $scope.texto = [];
        $scope.status = "";
        $scope.comment = [];
        $scope.comment = "";
        $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;
          }

        };
        $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.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(idPost){                              
                $.post(
                    "addComentarioRest.php",
                    {

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

    });
</script>

HTML HTML

<div id="postsApp" class="container" ng-app="postsApp" ng-controller="postsCtrl" ng-init="iniciaTimer()"> 


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


                    <textarea ng-model="comment" placeholder="Coloque aqui a mensagem..." class="form-control" class="form-control" rows="3" name="comment"></textarea>
                    <p><p><p><button ng-click="addComment(p.idPost)" class="btn btn-success btn-xs" type="button">Enviar</button>

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

Firstly I would suggest checking if $scope.texto holds your input data using debugger; 首先,我建议您使用调试器检查$ scope.texto是否保存您的输入数据。 or console.log(); 或console.log();

Secondly, you can check your $http.post() request. 其次,您可以检查$ http.post()请求。 Please see this thread, maybe something need to add headers. 请查看此线程,也许需要添加标题。 AngularJs $http.post() does not send data AngularJs $ http.post()不发送数据

Looks like you're assigning $scope.idpost to the value of the argument idPost for the idPost key in the request object in your addLike and addComment functions. 看起来你要指定$scope.idpost的参数值idPostidPost在你addLike和addComment功能请求对象的关键。

I also see you're using jQuery's AJAX helpers, but you should be using AngularJS's built in $http service to make POST requests within Angular. 我还看到您正在使用jQuery的AJAX帮助器,但是您应该使用AngularJS的内置$http服务在Angular中发出POST请求。

For example, for your comment saving function: 例如,对于您的评论保存功能:

$scope.addComment = function(idPost){
    $http.post("addComentarioRest.php", {
        "texto" :  $scope.texto,
        "idPost" : idPost
    })
    .then(function(dados) {
         if(dados.indexOf("OK") >= 0) {
             $scope.texto = "";
         } else {
             $scope.texto = "FALHOU";
         }
    });
};      

Definitely check out the AngularJS Docs on the $http service for more info: https://docs.angularjs.org/api/ng/service/$http 绝对可以在$http服务上查看AngularJS文档以获取更多信息: https : //docs.angularjs.org/api/ng/service/$http

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

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