This is my HTML:
<!doctype html>
<html ng-app='myApp'>
<head>
<title>My Angular App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="js/controllers.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Bree+Serif' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</head>
<body ng-controller='bodyController'>
<div class="heading">
<h1>My Social Website</h1>
<hr>
</div>
<div class="postsCss">
<div ng-repeat="post in posts | orderBy: '-votes'" class="mypost">
<a ng-show="post.link" href="{{post.link}}">{{post.title}}</a>
<span ng-hide="post.link"> {{post.title}}</span>
</br>
<li class="fa fa-thumbs-o-up" ng-click="upVote(post)"></li>
<li class="fa fa-thumbs-o-down" ng-click="downVote(post)"></li>
<span class="counter">{{post.votes}}</span><br>
<span ng-show="post.comment"> {{post.comment}} </span>
<br>
<form ng-submit="addComment(post)" class="myform">
<input type="text" placeholder="Add a Comment..." ng-model="comment"></input>
<button class="button" type="submit">Comment</button>
</form>
</div>
<h2>Add a New Post:</h2>
<form ng-submit="addPost()" class="myform">
<input type="text" placeholder="Add a Post" ng-model="title"></input><br>
<input type="text" placeholder="Link" ng-model="link"></input><br>
<button class="button" type="submit">Post</button>
</form>
</div>
</body>
</html>
and this is my Controller.js:
var app = angular.module('myApp', []);
app.controller('bodyController',
function($scope) {
$scope.posts = [
{ title: 'post 1', votes: 5 , comment: 'Very Nice' },
{ title: 'post 2', votes: 25, comment: 'good' },
{ title: 'post 3', votes: 55, comment: 'Very Nice' },
{ title: 'post 4', votes: 15, comment: 'Very Nice' },
{ title: 'post 5', votes: 26, comment: 'Very Nice' }
];
$scope.addPost = function(){
if(!$scope.title || $scope.title ==='') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
comment: 'Very Nice',
votes: 0
});
$scope.title = '';
$scope.link = '';
};
$scope.upVote = function(post){
post.votes += 1;
};
$scope.downVote = function(post){
if(post.votes <1) { return; }
post.votes -= 1;
};
$scope.addComment = function(post){
post.comment = $scope.comment;
};
})
But when I click the comment button; the comment which is already there for debugging purpose doesn't gets changed but get removed and I checked it some other way; the comment that was coming from the HTML was undefined.
commentan array ...then push new comment