Fill form dynamically with angularJS

1

In the system that I'm developing, it will have a input field for the user to search if such a CPF already exists registered in the Database, if I have I want to show all the data of that client in an already completed form

This is the function that does the search in the database and returns the query

$scope.PesquisarCpf = function () {

$http.post("http://GetCpf.php", {'cpf':$scope.cliente.cpf,}).
               success(function(data, status, headers, config){
                 $scope.contratos = data;
               }).error(function(data, status, headers, config){
                 location.href="#/Cliente"

               });
}
    
asked by anonymous 17.08.2015 / 00:46

2 answers

1

To fill in the value according to the template, just use the ngModel directive:

<input type="text" ng-model="contratos.cep" />
<input type="text" ng-model="contratos.cidade" />

Remembering that the value returned from PHP must be a JSON object.

    
17.08.2015 / 02:50
1

Imagine that you have a user's cpf Input in your form, and your "SearchPpf" function returns the user's data.

<input type="text" 
       ng-model="cpf"
       ng-model-options="{ debounce: 1000 }" // para ter um delay de um segundo para executar a função
       ng-change="PesquisarCpf(cliente.cpf)" />

In your scope I would only change the parameter pass, by good practices, never pass $ scope in the parameter, as you can see ng-change passes the parameter by the function call, you have scope access in the html itself. / p>

$scope.PesquisarCpf = function (cpf) {

$http.post("http://GetCpf.php", {"cpf": cpf,}).
               success(function(data, status, headers, config){
                 $scope.contratos = data;
               }).error(function(data, status, headers, config){
                 location.href="#/Cliente"

               });
}

If the example is not clear, I can create for you a more practical example in Plunker.

    
10.08.2016 / 14:59