我正在使用 AngularJS 创建一个应用程序,其中有一个通知列表,用户可以对这些通知发表评论。但是我不确定如何实现此评论功能。到目前为止,我有以下内容:
HTML:
<div ng-repeat="notice in notices"> <!-- details about notice --> <div ng-repeat="comment in notice.comments"> <p>{{comment.user}} - {{comment.text}}</p> <form ng-submit="addComment()"> <input type="text" ng-model="newComment.user"> <textarea ng-model="newComment.text"></textarea> <input type="submit" value="Add comment" /> </form> </div> </div>
所以本质上我是循环遍历通知,然后在循环内循环遍历评论。我还有一个用于添加新评论的表单,这对我来说是危险的地方。
AddComment()
将获取新的注释值,将其添加到现有的对象数组中,并通过工厂更新数据库。然而,尽管新值确实出现在 notification.comments 数组中,但我不确定如何更新评论列表以显示新添加的评论。
How could I monitor the state of the objects inside the second ng-repeat to accomodate for the changes presented by the form submit? With a directive?
最佳答案
您应该能够在提交时更新 Controller 内的 notice.comments
数组:
$scope.addComment = function() {
//update your DB, yada yada yada
//Update $scope.notices
$scope.notices[someIndex].comments.push(newComment)
}
关于javascript - 访问并刷新 ng-repeat 内的 AngularJS ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145076/