nour nour nour nour - 6 months ago 20
AngularJS Question

How to save an input value using AngularJS

I am working on a website that contains a field called comments where some particular users can post those comments.
My HTML code:



<form>
<input ng-model="stack"></input>
<button ng-click="save()">Save</button>
<p>Your comment: <span ng-bind="stack"></span></p>
</form>





The comments should be saved in to a variable then, posted in my web service which is a json file so I can do my Database calls. I have to use $http.post
I am using AngularJS and I have written my webservice using java (JAXB).



//Controller:
$scope.save = function() {
alert(name);
}
//In save I put alert to test because all the functions save I tested hadn't done what I need.
//server
$http({
method: 'POST',
url: 'url',
data: "stack="
});




Answer

$http.post does it for you.

This is the html code :

<form ng-submit="save()">
      <input ng-model="stack"></input>
      <button type="submit">Save</button>
      <p>Your comment:<span ng-bind="stack"></span></p>
</form>
This is the controller function :

function myController($scope,$http){
        $scope.save=function(){
            
      var data=$scope.stack;  
       /* post to server*/
        $http.post(url, data)
            .then(
           function(response){
          // success callback
                 }, 
           function(response){
           // failure callback
               });
                               
             }
        }