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.
This is the controller code and the html to show the stuff. 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. I'm still novice when it comes to angular and how ng-repeat works.
Also it is sending information to database, idPost and user ID, but the text is plain blank.
EDIT I removed the HTTP post part and all database requests are done in AJAX using timer. 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
<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>