How to pass an AngularJS value to the JAVA controller

-2

I have a form that sends a value (CPF) of a page to be checked for existence in the database. I use an angular controller to send the cpf to service and then to the controller.

Would you have any examples of how to do this?

HTMLCode        

</head><body><formmethod="POST">

      <div class="form-group">
        <label for="cpf" class="">CPF:</label>
        <input type="text" name="cpf" ng-model="cpf" >
      </div>

      <button ng-click="buscarCliente()">Buscar Cliente</button>

   </form>

</body>

</html>

Controller code

angular.module('').controller('TesteController', function ($scope, $timeout, MensagemFactory, ) {

var vm = $scope;

vm.buscarCliente = function(){
      if (vm.cpf != NULL) {
        alert("!");
  }
};

}

Services Code

angular.module('').service('Service', function (servers) {

    var service = {
      getCpf: getCpf
    };

    function getCpf(){
      return $http.get(servers.vendas + 'api/buscarCliente');
    }

    return service;
  });

Code: link

    
asked by anonymous 15.02.2017 / 01:48

1 answer

3

Good afternoon Alex,

Something I noticed in your code is that you are calling your function for a ng-click even though it has a form, I suggest you make your request on the submit of it. If I understood correctly what you meant, I'll give you an example:

var _SeuServico = ['$http', function($http) {


    function _getCpf() {
        return $http.get(servers.vendas + 'api/buscarCliente');
    }

    return {
        getCpf: _getCpf,
    };
}]; 

angular.module('').factory('SeuServico', _SeuServico);

Now for your controller you would inject your service and call it this way:

    SeuServico.getCpf().then(function(_data) {
        //Seu codigo
    });

Depending on your service, I recommend that you take a look at the angular promises.

Ref: link $ q

    
15.02.2017 / 15:36