2017-07-16 31 views
0

我目前正在嘗試做類似於facebook post/likes/comment system,到目前爲止我已經設法做帖子和喜歡,但我有問題的意見。AngularJS問題與ng重複和保存輸入

這是控制器代碼和html顯示的東西。問題是,當我試圖從ng-model =「texto」獲得第二個textarea的輸入時,它應該保存註釋並保存在範圍內,但似乎不起作用。當談到角度和ng-repeat的工作方式時,我仍然是新手。

此外,它正在向數據庫,idPost和用戶ID發送信息,但文本是空白的。

編輯我刪除了HTTP後部分和所有的數據庫請求都在AJAX使用計時器完成。發送到數據庫的評論問題仍然存在。

<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

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

回答

0

首先,如果$ scope.texto使用 調試握着你的輸入數據,我建議檢查;或console.log();其次,你可以檢查你的$ http.post()請求。請看這個線程,也許有些東西需要添加標題。 AngularJs $http.post() does not send data

+0

嗨感謝您的回覆,我不得不修改我的代碼因爲我必須使用AJAX,所以我刪除了HTTP部分並創建了一個計時器。至於調試器,我似乎無法讓它在netbeans – ruipascoal

+0

上工作,當您發送請求時,您可以在開發人員工具中檢查網絡選項卡以查看是否發送了任何參數? – mygeea

+0

我很抱歉,我在哪裏可以找到這些開發者工具?我的IDE是用葡萄牙語,所以它可能不同 – ruipascoal

0

看起來就像你分配$scope.idpost爲在你addLike和addComment功能請求對象idPost關鍵參數idPost的價值。

我也看到你正在使用jQuery的AJAX幫助程序,但是你應該使用AngularJS內建的$http服務來在Angular中發出POST請求。

例如,對於您的評論保存功能:

$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"; 
     } 
    }); 
};  

肯定檢查了AngularJS文檔的$http服務的更多信息:https://docs.angularjs.org/api/ng/service/$http

+0

另外,您還可以使用三元運算符減少成功回調中的代碼行數量: '$ scope.texto = dados.indexOf( 「OK」)> = 0? 「」:「FALHOU」' –

+0

嗨,謝謝你的回覆,我編輯了上面的代碼,因爲我必須使用AJAX來發布帖子,所以我刪除了HTTP並創建了一個計時器,以每隔1秒讀取帖子並關於idPost部分,它的工作,但需要一點清潔,我之前有一些問題,因爲當我點擊按鈕,它刷新了div,它不是刷新,但現在,這是一個正常工作 – ruipascoal