AngularJS how to put data and id when user send form

16 views
Skip to first unread message

Łukasz Ogan

unread,
Mar 24, 2016, 11:10:12 AM3/24/16
to AngularJS
I would like to send my api date and newsId together with data from form.
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? I do not want to keep data and id as html input. 

Sander Elias

unread,
Mar 25, 2016, 9:05:48 AM3/25/16
to AngularJS
Hi Łukasz,

Put it in your newcomm, for example during save, like this:

$scope.createComm = function(comment){
   comment
.data = data; //shouldn't this be date instead?
   
NewsModel.create(comment).then(function (result){
      initCreateComm
();
   
})
}

Then you are using 
var comm = this;
That code seems to have no purpose at all?

I think you should read through the styleguide, I think it will help you solving some issues.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages