簡體   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