这是我的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>
这是我的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;
};
})
但是当我点击评论按钮时;已经存在用于调试目的的注释不会被更改但会被删除,我会以其他方式检查它;来自HTML的评论未定义。
答案 0 :(得分:1)
所以问题是当你尝试设置它时你的$ scope.com是未定义的,我已经修复了它的一个plnkr,是的你想要在post对象中有一个数组其中包含对帖子和ng-repeat的所有评论,将显示所有评论。我将一个newComment字段标记到post对象上,其中注释等待推入到数组中,以下是对HTML的更改(您还需要通过$ index跟踪以确保重复的条目不会破坏您的代码!):< / p>
<span ng-show="post.comment" ng-repeat="comment in post.comment track by $index"> {{comment}} </span>
<br />
<form ng-submit="addComment(post)" class="myform">
<input type="text" placeholder="Add a Comment..." ng-model="post.newComment" />
<button class="button" type="submit">Comment</button>
</form>
</div
对帖子对象的更改:
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.addComment = function(post){
post.comment.push(post.newComment);
};
And heres a link to a working plnkr so you can see it in action!