2016-03-24 16 views
1

Benim api tarih ve newsId form verilerle birlikte göndermek istiyorum. Bu benim biçimidir:AngularJS kullanıcı form gönderdiğinde veri ve kimlik nasıl eklenir

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

Denetleyici:

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

    }) 

ve hizmet Verileri ve NewSID göndermek için bu kodu eklemek nasıl

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

? Verileri ve kimliği html girişi olarak tutmak istemiyorum.

cevap

1

Form angular.extend ile açıklama isteği oluşturmak üzerine gönderdikten sonra gönderebilir:

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

Exellent, teşekkürler! – lukassz

İlgili konular