2

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=" 
});

5
  • show your controller and service code. Commented May 31, 2016 at 9:03
  • 1
    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=" }); Commented May 31, 2016 at 9:09
  • provide it in the question. Not in the comments Commented May 31, 2016 at 9:10
  • I edited my question :) Commented May 31, 2016 at 9:14
  • 1
    put the $http inside the save method..... Commented May 31, 2016 at 9:18

2 Answers 2

4

$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
               });
                               
             }
        }

Sign up to request clarification or add additional context in comments.

5 Comments

I got this error: ReferenceError: $http is not defined at k.$scope.save whenever I try to save the input value
Have you put the $http on the header of your controller function like : " myController($scope,$http)" ?
function($scope, AppName) and AppName is my factory's name
So change it for function($scope, $http, AppName) for the injection.
@NotBad4U How can I store this as a java object? So I would be able to use this value in my classes
0
$scope.save = function (data1) {
            var value = JSON.stringify(data1);
            $http({
                method: 'POST',
                cache: false,
                url: serviceBasePath + '/api/save',
                data: value,
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                }
            }).success(function (data, status) {
                $('.modal-backdrop').remove();
                $scope.tableParams.reload();
                $scope.Form.$setPristine();
            }).error(function (data, status) {
                $scope.errors = [];
                $scope.errors.push(data);
            });
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.