How to pass input value with POST method using Angular

2

I am trying to pass certain values from a input through the POST method using Angular, however I am not able to pass those values in the URL and not even get them on the server. Could someone help me solve this problem?

Detail, for each button I click I have to pass the two values of the input + value of the button clicked (operation).

Form

<form id="form" ng-controller="Controller">
  <input id="val1" name="val1" ng-model="val1" placeholder="Valor 1" type="text">
  <input id="val2" name="val2" ng-model="val2" placeholder="Valor 2" type="text">
  <input id="operacao" name="operacao" type="hidden" value="0">
  <input id="soma" type="button" value="soma" ng-click="doClick(val1, val2)">
  <input id="sub" type="button" value="sub">
  <input id="mult" type="button" value="mult">
  <input id="divs" type="button" value="divs">
  <input id="resultado" type="text"><br>
</form>

Controller

.controller("Controller", function($scope, operacaoService) { $scope.model = {};
  $scope.doClick = function(operacao) {
    $scope.model.operacao = operacao;
    operacaoService.postOperacao($scope.model).success(function(data, status) {
      $scope.valores = data.result;
      console.log(data.result);
    }).error(function(data, status) {
      console.log("erro", status);
    });
  };
})

Service

module.factory('operacaoService', function($http) {

  var postOperacao = function(model) {

    return $http({
      url: "/operacoes",
      method: "post",
      params: model
    });
  };

  return {
    postOperacao: postOperacao
  };
});

Server (Node.js)

app.post('/operacoes', function(req, res) {

  var valores = req.query;
  var val1 = parseInt(valores.val1);
  var val2 = parseInt(valores.val2);
  var operacao = (valores.operacao);
  var result;

  if (operacao === "soma") {
    result = val1 + val2;
    res.send({
      result: result,
      val1: val1,
      val2: val2,
      operacao: operacao
    });

  }
    
asked by anonymous 20.01.2016 / 18:17

2 answers

1

In the ng-click call you are passing val1, val2 and the function receives operation
> You could send:

 <input id="soma" type="button" value="soma" ng-click="doClick(val1, val2, operacao)">

Passing the 3 parameters you want and getting into the function. The way he did he just took the operation. Home Or even:

<input id="val1" name="val1" ng-model="model.val1" placeholder="Valor 1" type="text">
<input id="val2" name="val2" ng-model="model.val2" placeholder="Valor 2" type="text">
<input id="operacao" name="operacao" ng-model="model.operacao" type="hidden" value="0">
<input id="soma" type="button" value="soma" ng-click="doClick(model)">

and the function would already pass the direct model. I did not test the rest of your code, but I saw that the values were not being passed. From a console.log inside the function to verify that the information is passing correctly

    
17.02.2016 / 19:26
3

Try to simplify your method $http

$http.post(url, data, config); //Você não está usando configurações extras, então não use o 3º parâmetro
$http.post('/operacoes', model);

If it still does not work, add a return to see what's happening. This could be a problem on the server.

$http.post('/operacoes',model).then( 
    function(res) {console.log(res)}
);

This will display the return on the console.

    
20.01.2016 / 18:31