Eternal loop boot using Angularjs

Asked

Viewed 89 times

0

I’m using a knob on which I have an ng-click=Search client call. It triggers a function in the controlled Angular but is in eternal loop. How to fix this ?

HTML

 <div class="botao" align="right">
              <md-button ng-class="loading ? 'btnprimario  md-button loading' : 'btnprimario  md-button'" type="submit" ng-click="buscarCliente()" ng-disabled="!cpfValido" class="btngreen btnBuscarCli">Buscar Cliente</md-button>
    </div>

Controller.JS

vm.buscarCliente = function(){
      vm.loading = true;
      if (vm.cpf !== undefined && vm.cpf !=='') {
        return desbloqueioCartaoService.buscarCPF(vm.cpf).then(function(response){
          if(response.status===500){
              MensagemFactory.setMensagem('teste');
          }
          if(response.status === 200){
            MensagemFactory.setMensagem('sucesso','Sucesso.','CPF encontrado com sucesso! '+vm.cpf,false,vm);
            vm.loading = false;
            parent.cliente = {};
            parent.cliente.leituraCartao = {};
            parent.cliente.leituraCartao.cpf = vm.cpf;
            $location.path('/leituraDeCartao');
          }else{
            MensagemFactory.setMensagem('info','Informação.',+vm.cpf+' Desculpe, Não foi encontrado.',false,vm);
          }
        }).catch(function() {
          MensagemFactory.setMensagem('erro','Error.',+vm.cpf+' Não foi encontrado.',false,vm);
        });
      } };

Imagery

Erro tela

  • Just put vm.loading = false in a Finally.

  • send the code for kindness!

  • has to impose a time in milliseconds ???

  • Note that at the beginning of your function, the variable vm.loading gets value true, but you only update it to false, when it gives status 200 (OK). You should update it to false when error also shows a message to the user. Debug your code, see if it is going through the correct stream and returning status 200, because if the Loader is appearing, the result is probably giving error.

  • can assign a delay to the button ?

  • @alexjosesilva, why assign a delay at the button? I see no need for that, as you have no way of knowing the exact time the request will take. As I said, you have to update the variable vm.loading for false in all returns from AJAX, so you ensure that the Loader will disappear from the button, regardless of return status.

Show 1 more comment

2 answers

1

Alex, to finish the loop just one call:

 return desbloqueioCartaoService.buscarCPF(vm.cpf).then(function(response){
     vm.loading = false;
     //seu codigo
 },function(_error) {
     vm.loading = false;
 });

What I suggest is to create an http browser by passing your message as a parameter.

If you happen to see an error, be it 500 or another, it will not enter your function unless you are treating before.

Follow an example:

$httpProvider.interceptors.push(['$window', '$q', function($window, $q) {
    var sessionInjector = {
        responseError: function(rejection) {
            if (rejection.status == 500) {
                //seu codigo                      
            } 

            return $q.reject(rejection);
        }
    };
    return sessionInjector;
}]);

Reference: https://docs.angularjs.org/api/ng/service/$http

0


Problem Answer

vm.buscarCliente = function(){
          vm.loading = true;
          if (vm.cpf !== undefined && vm.cpf !=='') {
            return desbloqueioCartaoService.buscarCPF(vm.cpf).then(function(response){
              if(response.status===500){
                  vm.loading = false;//finalizar o loop
                  MensagemFactory.setMensagem('teste');
              }
              if(response.status === 200){
                MensagemFactory.setMensagem('sucesso','Sucesso.','CPF encontrado com sucesso! '+vm.cpf,false,vm);
                vm.loading = false;//finalizar o loop
                parent.cliente = {};
                parent.cliente.leituraCartao = {};
                parent.cliente.leituraCartao.cpf = vm.cpf;
                $location.path('/leituraDeCartao');
              }else{
                MensagemFactory.setMensagem('info','Informação.',+vm.cpf+' Desculpe, Não foi encontrado.',false,vm);
              }
            }).catch(function() {
              vm.loading = false;//finalizar o loop
              MensagemFactory.setMensagem('erro','Error.',+vm.cpf+' Não foi encontrado.',false,vm);
            });
          } };

Browser other questions tagged

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