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

Just put
vm.loading = falsein a Finally.– Jéf Bueno
send the code for kindness!
– alexjosesilva
has to impose a time in milliseconds ???
– alexjosesilva
Note that at the beginning of your function, the variable
vm.loadinggets valuetrue, but you only update it tofalse, when it gives status 200 (OK). You should update it tofalsewhen 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.– Douglas Garrido
can assign a delay to the button ?
– alexjosesilva
@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 variablevm.loadingforfalsein all returns from AJAX, so you ensure that the Loader will disappear from the button, regardless of return status.– Douglas Garrido