javascript - 访问并刷新 ng-repeat 内的 AngularJS ng-repeat

标签 javascript angularjs directive

我正在使用 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/

相关文章:

javascript - 如何在博文中添加自动跳转符

javascript - React.js – 过滤多个indexOf

javascript - 尝试在我的网络应用程序中测试 google oauth 时出现 popup_closed_by_user 错误

javascript - Angular ng-repeat错误显示

angularjs - 对象 'directives' 未定义错误

Javascript用一些数组值动态初始化嵌套映射

javascript - 将 Nuxt 前端与 Flask 后端集成的最佳方法

javascript - 为什么 angular ng-bind 不从 JSON 中读取\n 换行符?

AngularJS : Listen for click events on a button outside of a directive

javascript - Jasmine Angular Directive(指令)被编译为一个对象