1
I am able to use to perform validations of empty input, min of characters and max of characters,example:
<form name="myForm">
<div class="input-group">
<input type="text" class="form-control" name="cep" ng-model="cep" ng-minlength="5" ng-maxlength="20" placeholder="Digite o CEP" autofocus required>
<span class="input-group-btn">
<button class="btn btn-success" type="submit" ng-click="buscar(cep)">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</span>
</div>
</form>
<div ng-messages="myForm.cep.$error">
<div ng-message="required" class="alert alert-danger" role="alert">Campo não pode ser vazio.</div>
<div ng-message="minlength" class="alert alert-danger" role="alert">Deve ser acima de 5 caracteres</div>
<div ng-message="maxlength" class="alert alert-danger" role="alert">Deve possuir menos que 20 caracteres</div>
</div>
Now I need to understand, how to use the same ng-message service to display a message in case my request fails example:
var app = angular.module("buscaCEP",['ngMessages']);
app.controller("buscaCEPCtrl",function($scope,$http){
$scope.buscar = function(cep){
$http.get('https://viacep.com.br/ws/'+cep+'/json/').success(function(data){
$scope.form = data;
}).error(function(){
console.log("Ocorreu um erro inesperado, verifique se os dados digitados estão corretos.");
});
};
});
No. http error need to send an action to display the console message.log();
Example working on plunker
That one
$dirty
I was trying to do it and I didn’t know how...– Gabriel Rodrigues