Ich habe Controller in AngularJS erstellt, um Kommentare zu meinem Ruhepunkt anzuzeigen und zu senden. Wie kann ich die Kommentarliste aktualisieren, nachdem der Benutzer geklickt und einen Kommentar gesendet hat?AngularJS Aktualisierungsansicht nach Sendekommentar
Mein Controller:
.controller('CommentController',
function($scope, $routeParams, NewsModel){
var newsId = $routeParams.id;
path = 'getCommetnsByNewsId/'+newsId;
var comm = this;
var data = new Date().toLocaleString();
$scope.createComm = function(comment){
NewsModel.createComment(angular.extend({},
{data: data, newsId: newsId}, comment)).then(function (result){
initCreateComm();
})
}
function initCreateComm(){
comm.newComm = { comment: '', author: ''};
}
NewsModel.getCommentById().then(function (result){
$scope.comments = result.data;
console.log($scope.comments);
});
})
Service:
.service('NewsModel', function($http, ENDPOINT_URI){
var service = this;
function getUrl(){
return ENDPOINT_URI + path;
}
service.all = function(){
return $http.get(getUrl());
}
service.getCommentById = function(){
return $http.get(getUrl());
}
service.createComment = function(comment){
return $http.post(getUrl(),comment);
}
});
Und HTML:
<!-- Comments Form -->
<div class="well">
<h4>Leave comment:</h4>
<form role="form" ng-submit="createComm(newComment)">
<div class="form-group">
<input type="text" class="form-control" ng-model="newComment.author" placeholder="Autor">
</div>
<div class="form-group">
<textarea class="form-control" ng-model="newComment.comment" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Wyślij</button>
</form>
</div>
<hr>
<div ng-repeat="comm in comments">
<div class="media">
<div class="media-body">
<h4 class="media-heading">{{comm.author}}
<small>{{comm.data}}</small>
</h4>
{{comm.comment}}
</div>
</div>
</div>
</div>
Wie dies zu tun?