繁体   English   中英

AngularJS 如何在用户发送表单时放置数据和 id

[英]AngularJS how to put data and id when user send form

我想将我的 api 日期和 newsId 与表单中的数据一起发送。 这是我的表格:

 <div class="well">
  <h4>Leave a Comment:</h4>
  <form role="form" ng-submit="createComm(newComment)" novalidate>
    <div class="form-group">
      <input type="text" class="form-control" placeholder="Autor" ng-model="newComment.author">
    </div>
    <div class="form-group">
      <textarea class="form-control" ng-model="newComment.comment" rows="3"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>

控制器:

.controller('CommentController',
    function($scope, $routeParams, NewsModel){

      var newsId = $routeParams.id;
      path = 'getCommetnsByNewsId/'+newsId;

      var comm = this;
      var data = new Date().toLocaleString();

      //comm.newComm.data = data; //this way?

      $scope.createComm = function(comment){
          NewsModel.create(comment).then(function (result){
            initCreateComm();
          })
      }

      function initCreateComm(){
         comm.newComm = { comment: '', author: '', data: '', id: ''};
      }

    })

和服务

 service.createComm = function(comm){
      return $http.post(getUrl(),comm);
    }

如何添加到此代码以发送数据和 newsId? 我不想将数据和 id 保留为 html 输入。

您可以在使用angular.extend创建评论请求的表单提交后发送它:

$scope.createComm = function(comment){
    NewsModel.create(angular.extend({}, {data: data, newsId: newsId}, comment)).then(function (result){
        initCreateComm();
    })
}

暂无
暂无

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

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