How to pass an Angularjs value to the JAVA controller

Asked

Viewed 569 times

-2

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

I’d have some example of how to do that ?

inserir a descrição da imagem aqui

HTML code

</head>

<body>
  <form method="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: https://jsfiddle.net/alexjosesilva/d48syn5c/

  • Please rewrite your question, paying attention to Portuguese errors and concordance.

  • What code do you already have ?

  • https://jsfiddle.net/alexjosesilva/d48syn5c/

  • In this series of video lessons of Diogo Godoi he develops a Javaee application with Angularjs explaining step by step communication between client and server. CRUD complete with communication also with data flock, very good in series, strongly recommend. https://www.youtube.com/playlist?list=PL1NdiP2jsLnuEqNkOF7-ISTciRrKd51Hv

1 answer

3


Good afternoon Alex,

Something I noticed in your code is that you are calling your function by an ng-click even having a form, I suggest you make your request in Submit of the same. If I understand what you mean, 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);

Already for your controller you would inject your service and make the call in this way:

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

Depending on your service I recommend you to take a look at the angular Promises.

Ref: https://docs.angularjs.org/api/ng/service/$q

Browser other questions tagged

You are not signed in. Login or sign up in order to post.