简体   繁体   English

如何修复 AngularJS 中的 ng-submit?

[英]How to fix ng-submit in AngularJS?

I am new to NodeJS and I'm trying to build a forum app using Angular with the ability to reply to comments.我是 NodeJS 的新手,我正在尝试使用 Angular 构建一个能够回复评论的论坛应用程序。 I'm able to add comments to a post.我可以在帖子中添加评论。 But I'm not able add replies to the comments of a post.但我无法对帖子的评论添加回复。 I think I'm doing something wrong with my controllers and routes.我想我的控制器和路线有问题。

Basically, I've tried implementing the reply function by looking at how the comment function was implemented for posts ie replaced post with comment and comment with reply.基本上,我已经尝试通过查看评论功能是如何实现的上岗即顶岗与评论和评论与回复执行回复功能。

My index.ejs, here I'm able to add comments to the post.我的 index.ejs,在这里我可以为帖子添加评论。

<script type="text/ng-template" id="/posts.html">

..............

<div ng-repeat="comment in post.comments | orderBy:'-upvotes'">
  <span style="font-size:20px; margin-left:10px;">
    {{comment.body}}
  </span>
</div>

<form ng-submit="addComment()" id= "my-form" style="margin-top:30px;">
  <h3>Add a new comment</h3>
    <div class="form-group">
      <input type="text" class="form-control" placeholder="Comment" ng-model="body"></input>
    </div>
  <button type="submit" class="btn btn-primary">Post</button>
</form>
</script>

To implement the reply functionality, I added the following code为了实现回复功能,我添加了以下代码

<div ng-repeat="reply in comment.replies">
      <span style="font-size:10px; margin-left:10px;">
        {{reply.replytext}}
      </span>
    </div>
    <form ng-submit="addReply()" style="margin-top:20px;">
      <h3>Add a new reply</h3>
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Reply" ng-model="replytext"></input> 
        </div>
        <button type="submit" class="btn btn-secondary">Reply</button>
    </form>
</div>

before

<form ng-submit="addComment()" id= "my-form" style="margin-top:30px;">

in my index.ejs.在我的 index.ejs 中。 When I do this, clicking on the reply button does nothing.当我这样做时,点击回复按钮什么也不做。

The following is what is present in my appAngular.js以下是我的 appAngular.js 中的内容

app.factory('posts', ['$http', function ($http) {
    var o = {
        posts: []
    };
    // .......

    o.addComment = function (id, comment) {
        return $http.post('/posts/' + id + '/comments', comment);
    };

    // .........

    return o;
}]);

app.controller('PostsCtrl', [
    '$scope', 'posts', 'post',
    function ($scope, posts, post) {
        $scope.post = post;
        $scope.addComment = function () {
            if (!$scope.body || $scope.body === '') { return; }
            posts.addComment(post._id, {
                body: $scope.body,
                author: 'user',
            }).success(function (comment) {
                $scope.post.comments.push(comment);
            });
            $scope.body = '';
        };

        // .......

    }]);

I wrote the code for o.addReply() similar to o.addComment() which I think is incorrect.我为 o.addReply() 编写了类似于 o.addComment() 的代码,我认为这是不正确的。

The following is my routes.js,以下是我的routes.js,

router.post('/posts/:post/comments', function (req, res, next) {
    var comment = new Comment(req.body);
    comment.post = req.post;

    comment.save(function (err, comment) {
        if (err) { return next(err); }

        req.post.comments.push(comment);
        req.post.save(function (err, post) {
            if (err) { return next(err); }
            res.json(comment);
        });
    });
});

router.param('comment', function (req, res, next, id) {
    var query = Comment.findById(id);

    query.exec(function (err, comment) {
        if (err) { return next(err); }
        if (!comment) { return next(new Error('can\'t find post')); }

        req.comment = comment;
        return next();
    });
});

For implementing replies, I added the following to my routes.js为了实现回复,我在 routes.js 中添加了以下内容

router.post('/posts/:post/comments/:comment/replies', function (req, res, next) {
    var comment = new Comment(req.body);
    comment.post = req.post;

    var reply = new Reply(req.body);
    reply.comment = req.comment;

    reply.save(function (err, reply) {
        if (err) { return next(err); }

        req.post.comments.comment.replies.push(reply);
        req.post.comments.coment.save(function (err, comment) {
            if (err) { return next(err); }

            res.json(reply);
        });
    });
});

router.param('reply', function (req, res, next, id) {
    var query1 = Reply.findById(id);

    query1.exec(function (err, reply) {
        if (err) { return next(err); }
        if (!reply) { return next(new Error('can\'t find comment')); }

        req.reply = reply;
        return next();
    });
});

Finally, this is my mongoose model最后,这是我的猫鼬模型

var CommentSchema = new mongoose.Schema({
    body: String,
    author: String,
    upvotes: { type: Number, default: 0 },
    post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' },
    reply: { type: mongoose.Schema.Types.ObjectId, ref: 'Reply' }
});

    .........

var ReplySchema = new mongoose.Schema({
    comment: { type: mongoose.Schema.Types.ObjectId, ref: 'Comment' },
    replytext: String
});

mongoose.model('Comment', CommentSchema);
mongoose.model('Reply', ReplySchema);

Check this line of code in your appAngular.js file检查 appAngular.js 文件中的这行代码

return $http.post('/posts/' + post._id + '/comments/' + comment._id ++ '/replies', reply);

There is a double + sign in your route address.您的路线地址中有一个双+号。 It can be source of your problem.它可能是您问题的根源。

Try this:尝试这个:

<form action="/method2"  method="post" style="margin-top:20px;">
      <h3>Add a new reply</h3>
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Reply" ng-model="replytext"></input>

        </div>
        <button type="submit" value="accept" class="btn btn-secondary">Reply </button>
    </form>
</div>

<form action="/method1"  method="post" style="margin-top:30px;">
  <h3>Add a new comment</h3>
    <div class="form-group">
      <input type="text" class="form-control" placeholder="Comment" ng-model="body"></input>
    </div>
  <button type="submit" class="btn btn-primary">Post</button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM