Why do the form data disappear when pulling the zip code?

Asked

Viewed 40 times

-1

I made a data editing screen, and the problem that is occurring is that when I put the zip code the address data appears, but the data, already filled out disappear! Can someone help me?

HTML:

<form name="formPerfilStartup">
     <div class="row">
         <div class="col-sm-12 col-md-12">
            <p id="cep">CEP</p>
            <input class="campoCep" type="text" name="cep" ng-model="perfilNovaStartup.cep">
            <p id="cidade">Cidade</p>
            <input class="campoCidade" type="text" name="localidade" ng-model="perfilNovaStartup.localidade">
            <p id="estado">Estado</p>
            <input class="selectEstado" type="text" name="uf" id="" ng-model="perfilNovaStartup.uf">
            </div>

            </div>

            <div class="row">
                <div class="col-sm-6 col-md-6">
                    <p id="startup">Nome Startup</p>
                    <input class="startup" type="text" name="nomeStartup" ng-model="perfilNovaStartup.nomeStartup">
                </div>
                <div class="col-sm-6 col-md-6 ">
                    <p id="descricao">Descriçãp (resumo)</p>
                    <textarea name="resumo" cols="49" rows="5" ng-model="perfilNovaStartup.resumo" 
                    placeholder="Sou engenheiro elétrico a 10 anos...">

                    </textarea>
                </div>
            </div>
    </form>

JS that brings the cep data and other data:

app.controller("editarPerfilStartupCtrl", ['$scope', '$http', '$window', '$location', '$rootScope', '$routeParams', function ($scope, $http, $window, $location, $rootScope, $routeParams) {

$scope.idusuario = localStorage.getItem("startwe_idusuario");
$scope.usuario = localStorage.getItem("startwe_usuario");
$scope.idstartup = $routeParams.idstartup;
console.log(`id usuario ${$scope.idusuario}, usuario ${$scope.usuario}, id startup ${$scope.idstartup}`)

if(location.hostname == 'localhost'){
    var urlPrefix = 'http://localhost:8888/sistemas/Webapps/Projetos/startWe/api/registerStartup.php';
    var urlOptionPrefix = 'http://localhost:8888/sistemas/Webapps/Projetos/startWe/api/registerStartup.php?option=';
} else {
    var urlPrefix = '';
}

$(document).ready(function(){

    $('.campoCep').focusout(function(){
        var valorCep = $('.campoCep').val();
        $http.get('http://viacep.com.br/ws/'+valorCep+'/json/').then(function(response){
            if(response.data.erro == true){
                $scope.erro = 'CEP inexistente! Tente novamente.';
            }
            $scope.perfilNovaStartup = response.data;
        })
    })

});

var pegarStartup = function(){
    var option = 'pegar startup';
    var idstartup = $scope.idstartup;
    $http.get(urlOptionPrefix + option + '&idstartup=' + idstartup).then(function(response){
        //console.log(response.data)
        $scope.perfilNovaStartup = response.data;
    })

}
pegarStartup();

}]);

1 answer

1


From what I saw of your code, you update all the contents of the "profileNovaStartup" model when you perform the "pegStartup" function or when you search the zip code. That’s why when you search the zip code you delete the name and the summary.

To avoid this problem, when searching the ZIP code, you should change only the model fields referring to the ZIP code. As I do not have the return of research, I imagine that the change after returning the zip code is like this:

$(document).ready(function(){

    $('.campoCep').focusout(function(){
        var valorCep = $('.campoCep').val();
        $http.get('http://viacep.com.br/ws/'+valorCep+'/json/').then(function(response){
            if(response.data.erro == true){
                $scope.erro = 'CEP inexistente! Tente novamente.';
            }
			$scope.perfilNovaStartup.cep = response.data.cep;
			$scope.perfilNovaStartup.localidade = response.data.localidade;
			$scope.perfilNovaStartup.uf = response.data.uf;
        })
    })

});

Browser other questions tagged

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