I have a similar code like the one below for a registration form. In this example the email value is preload therefor the error is not shown. But how can I avoid the error when there is no preload value, in other words when the value is empty like:
$scope.Email = {valor:""};
Here is the example:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example59-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular-animate.js"></script>
</head>
<body ng-app="formExample">
<script>
angular.module('formExample', [])
.controller('FormController', ['$scope',
function($scope) {
$scope.Email = {
valor: "a@a.com"
};
}
]);
</script>
<style>
.my-form {
transition: all linear 0.5s;
background: transparent;
}
.my-form.ng-invalid {
background: red;
}
</style>
<form name="myForm" ng-controller="FormController">
Email:
<input name="input" ng-model="Email.valor" required class="my-form">
<span class="error" ng-show="myForm.input.$error.required">Required!</span>
<br>
<code>userType = {{Email.valor}}</code>
<br>
<code>myForm.input.$valid = {{myForm.input.$valid}}</code>
<br>
<code>myForm.input.$error = {{myForm.input.$error}}</code>
<br>
<code>myForm.$valid = {{myForm.$valid}}</code>
<br>
<code>myForm.$error.required = {{!!myForm.$error.required}}</code>
<br>
</form>
</body>
</html>