简体   繁体   English

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

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

I would like to send my api date and newsId together with data from form.我想将我的 api 日期和 newsId 与表单中的数据一起发送。 This is my form:这是我的表格:

 <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:控制器:

.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: ''};
      }

    })

and service和服务

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

How can I add to this code to send data and newsId?如何添加到此代码以发送数据和 newsId? I do not want to keep data and id as html input.我不想将数据和 id 保留为 html 输入。

You could send it after form submitting on create comment request with angular.extend :您可以在使用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.

相关问题 AngularJS:PUT使用URL发送数据,但不作为JSON数据发送 - AngularJS: PUT send data with URL but not as JSON data AngularJS&#39;PUT&#39;请求。 如何只提交表格中的数据? - AngularJS 'PUT' requests. How to submit ONLY the data in form? 如何在 angularjs 中进行表单验证 - How to put form validation in angularjs 如何使用Angularjs和PHP发送带有表单数据和附件的电子邮件 - How to Send Email With Form data and Attachment Using Angularjs and PHP 如何使用AngularJS表单将POST数据发送到Play Framework Controller - How to send POST data with an AngularJS form to Play Framework Controller 如何在angularjs中发送多个文件以及表单数据 - How to send Multiple FIles along with the form data in angularjs 如何使用AngularJS $ http发送multipart / form-data - How to use AngularJS $http to send multipart/form-data AngularJS - 当用户输入错误的URL时$ stateProvider - AngularJS - $stateProvider when user put wrong URL 如何将表单数据从前端发送到后端,以便当用户登录时他们可以登录 - How do I send form data from frontend to backend so when user is at login they can login 我如何获取用户数据并将 output 放入表单 | javascript - How can i get user data and put the output in the form | javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM