Display Alert according to an Ionic condition

3

I have a user registration page where I have a code field where I would like to validate it according to a condition ($ scope.erro) coming from my controller, I already have some alerts but they validate only the form / p>

Controller:

 if(status === 406){
        $scope.erro = true;
        console.log("Erro");
  }

This is the section of the button, followed by the template that displays the errors:

<label class="item item-input" style="margin-left:10px;margin-right:10px;margin-top:8px"
   ng-class="{'has-errors' : usuarioForm.codigo.$invalid, 'no-errors' : usuarioForm.codigo.$valid}">
<input type="text" name="codigo" placeholder="Código" ng-model="usuario.codigo" ng-minlength="3" ng-maxlength="20"
   ng-blur="getUsuario(usuario, $event)" required>
</label>

<div class="error-container" ng-show="usuarioForm.codigo.$error" ng-messages="usuarioForm.codigo.$error">
   <div ng-messages-include="error-list.html"></div>
</div>

Template:

<script id="error-list.html" type="text/ng-template">
   <div class="error" ng-message="required">
   <i class="ion-information-circled"></i>
   ESSE CAMPO É OBRIGATÓRIO
   </div>

   <div class="error" ng-message="erro">
   <i class="ion-information-circled"></i>
   TESTE
   </div>

   <div class="error" ng-message="minlength">
   <i class="ion-information-circled"></i>
   Minimum length of this field is 5 characters!
   </div>
   <div class="error" ng-message="maxlength">
   <i class="ion-information-circled"></i>
   Maximum length of this field is 20 characters!
   </div>
</script>

In short, I want to display a message when the $scope.erro variable is true.

Example page:

    
asked by anonymous 26.10.2015 / 19:17

1 answer

3

I solved the problem as follows:

No controller:

$scope.showAlert = function(titulo, erroMsg) {
    var alertPopup = $ionicPopup.alert({
      title: titulo,
      template: erroMsg
    });
    alertPopup.then(function(res) {
      console.log('Err');
    });
  };

Then just call the showAlert function and pass the title and error message, eg:

var erroMsg = "Código Inválido!";
var titulo = "Erro ao entrar com o código";
$scope.showAlert(titulo, erroMsg);

This example above shows a pop up with the error, however to put the error under the input I did it like this:

<form name="usuarioForm" role="form" novalidate="">

<label class="item item-input" style="margin-left:10px;margin-right:10px;margin-top:8px"
ng-class="{'has-errors' : erro, 'no-errors' : !erro}">

<input type="text" name="codigo" placeholder="Código" ng-model="usuario.codigo" ng-minlength="3" ng-maxlength="20"
ng-blur="getUsuario(usuario, $event)" required>
</label>
<div class="error-container" ng-show="erro">
<div class="error">
<i class="ion-information-circled"></i>
CÓDIGO INVÁLIDO
</div>
</div>

<label class="item item-input" style="margin-left:10px;margin-right:10px;margin-top:5px">
<input type="text" placeholder="Nome" ng-model="usuario.nome" ng-required="true">
</label>

<label class="item item-input" style="margin-left:10px;margin-right:10px;margin-top:5px">
<input type="text" placeholder="Login" ng-model="usuario.login" ng-required="true">
</label>

I'm showing the invalid code message if any error occurs in Controller , so just pass $scope.erro to true .

Ex:

error(function(response, status) {
    if (status === 406) {
        $scope.erro = true;
        var erroMsg = "Código não encontrado! Tenve novamente";
        var titulo = "Erro ao verificar código";
        $scope.showAlert(titulo, erroMsg);
        console.log("Erro");
    }
});
    
04.01.2016 / 11:27